If I first place something into the lua's registry table with:
int ref = luaL_ref(L, LUA_REGISTRYINDEX);
Then unreference ref
with:
luaL_unref(L, LUA_REGISTRYINDEX, ref);
and start the garbage collector with:
lua_gc(L, LUA_GCCOLLECT, 0);
I can still see the ref
entries in the registry table when I print it up. Why does luaL_unref
not remove the unreferenced entries, so that the gc could collect them?
As @siffiejoe noted in comments, Lua's auxiliary library uses "freelist" mechanism to keep track of free index holes, and that list never shrinks, i.e. its size is always equal to peak allocation — that is size-to-speed tradeoff. You may check the implementation to figure out underlying logic.
If you are sure that no active refs remaining in registry, you may clear integer keys manually (see note below). If you want a completely empty registry, then lua_newtable(L), lua_replace(L, LUA_REGISTRYINDEX)
will do that. This is not a good idea though — see comments below this answer.
(Note that you probably can't simply check if all refs were balanced, because some code might ref an integer value. I may be wrong on this though.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With