[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: crash in Lua 5.1 and 5.2: calling C function from unloaded lib
- From: Josh Haberman <jhaberman@...>
- Date: Sun, 8 Apr 2012 17:58:12 -0700
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