lua-users home
lua-l archive

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


> On 2021-11-28 12:13, Ranier Vilela wrote:
> 
> > You have to make sure that no library code is executed after dlclose.
> 
> Thanks. I guess I have to find out what calls dlclose. My belief is that it
> is only called when the Lua
> thread that required the C-module is shut down. If that is the case there
> is no problem.

dlclose is called when you close the Lua state, calling lua_close.

The package loadlib creates a table in the registry, where it puts
all C packages it loads. We can see this table with this code:

  print(debug.getregistry()._CLIBS)    -- for Lua 5.4

This table has a metamethod __gc that dlclose's all libraries in that
table. As the table is in the registry, it is never collected, so that
it is called only when we close the Lua state.

Because this table is created and marked before we can load any C
module, it will be collected after the collection of any object
created by any C module.

-- Roberto