Hi,
I load a Lua C library called testlib.dll using require("testlib") and I want to unload it at runtime. This is because I need to load a different version of it from another path. So basically this is a library with the same name but two different paths.
I do the following currently in Lua when unloading the library:
package.loaded.testlib
= nil
I also nil any symbols its created and nothing holds any references to the library's functions. So I can unload it safely.
After that I force a DLL unload in Windows using:
void forceUnloadLibrary(std::string path)
{
BOOL ret;
HMODULE hMod;
do {
hMod = GetModuleHandleA(path.c_str());
if (hMod != NULL) {
ret = FreeLibrary(hMod);
}
} while (hMod != NULL && ret != 0);
std::cout << "Unloaded: " + path << "\n";
}
Nothing is holding a reference to the library in the C side either, so I can safely do the above. The issue is when loading this module again. I do a require("testlib") again, and the following function still finds a pointer to the C library and so its not loaded again:
/*
** return registry.CLIBS[path]
*/
static void *checkclib (lua_State *L, const char *path) {
void *plib;
lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS);
lua_getfield(L, -1, path);
plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */
lua_pop(L, 2); /* pop CLIBS table and 'plib' */
return plib;
}
So how can I safely clear the
LUA_REGISTRYINDEX (
registry.CLIBS[path] specifically), so that
plib is set to NULL in the function and the DLL is again loaded in lsys_load() and the open function in it is called?
Also is there any other cleanup I need to do when unloading the C library, so it works exactly as if it has been loaded the first time :)
Thanks,
Abhijit