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.