Hi all,
I am new here but an experienced lua user (both the C side and the lua syntax). I am using the cffi-lua library (
https://github.com/q66/cffi-lua) in lua 5.4.6
Sometimes, but not always, the
cffi.new() call (which calls luaL_setmetatable to set a userdata metatable with a __gc method as the meta table), triggers a null pointer exception in luaC_checkfinalizer (which is called by lua_setmetatable). Also see the call stack at the bottom of this message.
It happens on the line (see snippet below for the whole function body).
for (p = &g->allgc; *p != o; p = &(*p)->next) { /* empty */ }
Which as I understand it, implies no reference to o was found inside allgc. What went wrong here?
Best of wishes,
Gynt
/*
** if object 'o' has a finalizer, remove it from 'allgc' list (must
** search the list to find it) and link it in 'finobj' list.
*/
void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
global_State *g = G(L);
if (tofinalize(o) || /* obj. is already marked... */
gfasttm(g, mt, TM_GC) == NULL || /* or has no finalizer... */
(g->gcstp & GCSTPCLS)) /* or closing state? */
return; /* nothing to be done */
else { /* move 'o' to 'finobj' list */
GCObject **p;
if (issweepphase(g)) {
makewhite(g, o); /* "sweep" object 'o' */
if (g->sweepgc == &o->next) /* should not remove 'sweepgc' object */
g->sweepgc = sweeptolive(L, g->sweepgc); /* change 'sweepgc' */
}
else
correctpointers(g, o);
/* search for pointer pointing to 'o' */
for (p = &g->allgc; *p != o; p = &(*p)->next) { /* empty */ }
*p = o->next; /* remove 'o' from 'allgc' list */
o->next = g->finobj; /* link it in 'finobj' list */
g->finobj = o;
l_setbit(o->marked, FINALIZEDBIT); /* mark it as such */
}
}
Call stack:
lua.dll!luaC_checkfinalizer(lua_State * L, GCObject * o, Table * mt) Line 1033 C
lua.dll!lua_setmetatable(lua_State * L, int objindex) Line 958 C
lua.dll!luaL_setmetatable(lua_State * L, const char * tname) Line 326 C
cffi.dll!lua::mark_cdata(lua_State * L) Line 123 C++
cffi.dll!ffi::newcdata(lua_State * L, const ast::c_type & tp, unsigned int vals) Line 234 C++
cffi.dll!ffi::make_cdata(lua_State * L, const ast::c_type & decl, int rule, int idx) Line 1614 C++
> cffi.dll!ffi_module::new_f(lua_State * L) Line 1097 C++