[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: luaclose_mylib function (inverse of "luaopen_mylib" C function)
- From: Andrew Gierth <andrew@...>
- Date: Tue, 25 Aug 2020 12:43:10 +0100
>>>>> "bel" == bel <bel2125@gmail.com> writes:
bel> Is there any way the library could ever be unloaded, before the
bel> Lua state is destroyed?
What Lua does with dynamically loaded libraries is this: there is a
table stored in the registry which contains the pathnames and handles of
all dynamically opened objects, and this table has a __gc metamethod
that closes all of those handles. Since this is in the registry and has
had its finalizer set before loading any library, and since finalizers
are called in reverse order of setting, this means that libraries won't
be unloaded until after any finalizers that they set themselves have
run, if I understand things correctly.
There is no way to "unrequire" a module short of closing the state
(unless you do really dangerous things in C code, like replacing the
registry table or the CLIBS table in the registry, or calling its __gc
method explicitly).
If there are multiple Lua states, it's up to the OS facilities being
used to load dynamic libraries to do the necessary reference-counting so
that a library opened multiple times is not unloaded until all
references are closed.
--
Andrew.