lua-users home
lua-l archive

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

> While this library is doing nothing fancy by itself, other libraries in the
> system are using a way to register that is using the environment.
> static void InitializeBridge(lua_State* L, const char* libraryName, const
> luaL_Reg* reg = NULL)
> {
> // since we're setting an environment, we need to call through Lua to set it
> up
> lua_pushcfunction(L, __ActualLibraryInitialization);
> lua_pushstring(L, libraryName);
> lua_pushlightuserdata(L, (void*)reg);
> lua_call(L, 2, 1);
> }
> /**
> Performs the actual initialization of the library. This function should only
> be called through InitializeBridge
> */
> static int __ActualLibraryInitialization(lua_State* L) {
> const char* libraryName = luaL_checkstring(L, 1);
> luaL_checktype(L, 2, LUA_TLIGHTUSERDATA);
> const luaL_Reg* reg = (const luaL_Reg*)lua_touserdata(L, 2);
> static const struct luaL_reg dummy[] = {
> };
>  // create a table and use it as the environment for this module
> lua_newtable(L);
> lua_replace(L, LUA_ENVIRONINDEX);
>  // register module functions. This leaves a table on top of the stack
> if(reg) {
> luaL_register(L, libraryName, reg);
> }
> else {
> luaL_register(L, libraryName, dummy);
> }
> return 1;
> }
> Other libraries using this method to setup an environment do use the
> environment as a metatable for a sentinel data (which is used to tell when
> the library is unloaded). Is it possible that I'm mistakingly overriding the
> __gc method of another library?

I did not understand the relationship between the above code and the
description. You talk about a "sentinel data", but I did not find
where you are creating it in the code (it should be a full userdata
to have its __gc called).

Without seing the relevant code, I would say that the most probable
cause of your problem is that you are creating the sentinel data
*before* opening the library. "The finalizers for userdata are called in
reverse order of their creation" (ref manual, 2.10.1). So, if you
create the userdata before opening the library, the library will be
closed before the invocation of the __gc of that userdata.

-- Roberto