Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I clean up lua's registry?

Tags:

c

lua

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?

like image 670
user1095108 Avatar asked Dec 13 '14 15:12

user1095108


1 Answers

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.)

like image 197
user3125367 Avatar answered Oct 04 '22 16:10

user3125367