[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Unloading a library loaded via package.loadlib()
- From: Thomas Lauer <thomas.lauer@...>
- Date: Fri, 25 Jul 2008 12:57:31 +0100
In an environment where a single instance of Lua (ie one lua_State) runs
for an extended period of time, I sometimes have to unload modules I've
require()'d before. That's straightforward if the module in question is
pure Lua.
However, a number of my libraries are mixed in that they require() a Lua
module which in turn calls package.loadlib() to dynamically load a
library. Upon unloading the module the dynamically loaded library will
get unloaded via a call to its attached __gc handler.
However, this __gc handler is not necessarily called after a module has
been unloaded and no amount of collectgarbage() calls will *reliably*
convince the GC that there is a userdatum to collect (which would free
the library). Sure, the __gc handler is *eventually* called, eg when the
Lua instance terminates -- but I have to make sure the library is
unloaded the moment the Lua module is unloaded.
I have found no "official" Lua function to explicitly unload DLLs (this
is Windows, but I would think the same problem exists for other
platforms with dynamic loading). It seems I can grab the reference to
the DLL from the registry, trigger the unloading manually and invalidate
the handle, so that the __gc handler, when it's called later, stays
happy. This should work alright but it has all the hallmarks of a bad
kludge.
Did I overlook something? If not, perhaps 5.2's package system could
include an explicit function for that purpose, if only for consistency's
sake.
--
cheers thomasl
web: http://thomaslauer.com/start