lua-users home
lua-l archive

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


Hi everyone. I'm experiencing a crash when a C module is unloaded. I think I know what is the cause but it seems strange that it is failing.

Part of the relevant code below:

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

extern "C" __declspec(dllexport) int luaopen_mymodule(lua_State* L) {
  /* env.__gc = module_cleanup */
  lua_pushcclosure(L, module_cleanup, 0);
  lua_setfield(L, LUA_ENVIRONINDEX, "__gc");
  
  lua_newtable(L);
  return 1;
}


The thing is that when the Lua state is closed, the module is unloaded first and then the function 'module_cleanup' is (tried to be) called.
Obviously, when I don't assign a __gc field to the environment the problem does not occur.

Does this mean that the environment is its own metatable? If not, is this a bug?

Regards,
Ignacio