Home
Reading
Searching
Subscribe
Sponsors
Statistics
Posting
Contact
Spam
Lists
Links
About
Hosting
Filtering
Features Download
Marketing
Archives
FAQ
Blog
 
Gmane
From: Josh Haberman <jhaberman <at> gmail.com>
Subject: crash in Lua 5.1 and 5.2: calling C function from unloaded lib
Newsgroups: gmane.comp.lang.lua.general
Date: Monday 9th April 2012 00:58:12 UTC (over 5 years ago)
The following extension and Lua program crash both Lua 5.1 and Lua
5.2.  From an strace it appears that the extension library is called
into after it was unloaded.  Perhaps this is a GC-related bug where
the loaded library is collected prematurely?

--

#include "lauxlib.h"

#if LUA_VERSION_NUM == 501
#define newlib(L, name, l) luaL_register(L, name, l)
#elif LUA_VERSION_NUM == 502
#define newlib(L, name, l) luaL_newlib(L, l)
#endif

static int nop(lua_State *L) { return 0; }

static const struct luaL_Reg funcs[] = {
  {"nop", nop},
  {NULL, NULL}
};

int luaopen_ext(lua_State *L) {
  newlib(L, "ext", funcs);
  return 1;
}

--

if _VERSION >= 'Lua 5.2' then
  function atexit(fn)
    setmetatable({}, { __gc = fn })
  end
else
  function atexit(fn)
    getmetatable(newproxy(true)).__gc = fn
  end
end

atexit(function() func() end)

local ext = require "ext"

function func() ext.nop() end
 
CD: 3ms