How can I clean the lua registry?

If I first put something in the lua registry table with:

int ref = luaL_ref(L, LUA_REGISTRYINDEX); 

Then unreference ref with:

 luaL_unref(L, LUA_REGISTRYINDEX, ref); 

and run the garbage collector:

 lua_gc(L, LUA_GCCOLLECT, 0); 

I still see the ref entries in the registry table when I print it. Why luaL_unref n't luaL_unref delete unwritten entries so gc can collect them?

+6
source share
1 answer

As @siffiejoe notes in the comments, the Lua helper library uses the β€œfreelist” mechanism to track free index holes, and this list is never compressed, i.e. its size is always equal to the peak distribution - this is the ratio of price and speed, you can check the implementation to determine the basic logic.

If you are sure that there are no active links in the registry, you can clear the whole keys manually (see the note below). If you want a completely empty registry, then lua_newtable(L), lua_replace(L, LUA_REGISTRYINDEX) will do this. This is not a good idea - see the comments below this answer.

(Note that you probably can't just check if all refs links were balanced, because some code might refer to an integer value. Maybe I'm wrong.)

+1
source

Source: https://habr.com/ru/post/979541/


All Articles