lua-users home
lua-l archive

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


Hi all:

I experienced something strange while running scripts in "child" states from
a master state.  Essentially, I was doing this to load a script and save
it's entry point:
                                                                         
    luaL_loadfile(childState, fileName);
    entryPointRef = luaL_ref(childState, LUA_REGISTRYINDEX);

However, this seems to have created a reference to childState such that it
was never completely garbage collected.  I could see lua_getgccount()
constantly growing as I reloaded/ran the script.  Then, I learned to do
this:

    luaL_unref(childState, LUA_REGISTRYINDEX, entryPointRef);

Viola!  The memory usage stops growing and GC seems to always work back to a
"steady state".  Made sense to me.  The script can now be GC'd because
nobody holds a reference to it.

EXCEPT... on occasion, the script seems to be GC'd before I do the unref()
and it crashes when I get to the unref().  Can anybody give me a hint about
this.  Am I approaching this totally wrong?

I managed to solve the problem by creating my references in the "master"
state, but I'd still like to understand what is going on under the hood.

Many thanks,

Matt