lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I am using the following code to load a Cfunction pointer for a submodule, loading the submodule if necessary. The problem I encounter is in the endgame, when in a __gc function e also a function from such a subsystem is called; a subsystem -- Iam sure because I checked -- earlier on has been loaded. From __gc I get a segmentation fault:

cfunload close.iom 0x5d500
/Users/hans/Projects/Lua/IOmodule/build/IOmodule.build/Debug/ ioio.build/Script-3A1B99740E447D1A00D7CC5E.sh: line 5: 3695 Segmentation fault /usr/local/bin/lua test.lua

My guess is that the function pointer close.iom at 0x5d500 is no longer valid, presumably because gc had it dumped, at a moment the entry in the global environment not being cleared. Or anything along that line.

lua_getglobal(L, iosystem == NULL ? MODULENAME : iosystem);/* table */
if (lua_isnil(L,-1))		/* nil means: not loaded yet */
{
  lua_pop(L, 1);		/* pop the previous nil */
  lua_getglobal(L, "require");	/* setup call to require */
lua_pushstring(L, iosystem); /* argument is name of module to load */
  if (lua_pcall(L, 1, 1, 0))	/* call and process error */
    luaL_error(L, "require \"%s\" failed", lua_tostring(L, -1));
  fprintf(stderr, "loaded subsystem %s\n", iosystem);
}
/* From this point on the subsystem table is found on top of the stack. */
lua_getfield(L, -1, func);	/* retrieve C function pointer */
lua_remove(L, -2);		/* remove the table or it gets in the way */

My question: can anyone more versed in the internals of Lua tell me if this could indeed be the case? Is there a solution or must I absolutely refrain from calling other modules during the gc cleanup phase?

Thanks in advance
Hans van der Meer