Bug 9900 – static this and gc shutdown order issue
Status
RESOLVED
Resolution
WONTFIX
Severity
normal
Priority
P2
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-04-07T15:17:00Z
Last change time
2013-04-08T05:40:15Z
Assigned to
nobody
Creator
flamaros.xavier
Comments
Comment #0 by flamaros.xavier — 2013-04-07T15:17:16Z
It seems gc is called after the main function to release all allocation, but only after all static this method were called.
I get a crash when my application is shutting down because Derelict library use static this methods to unload dynamic libraries, but I have to use some functions of those dynamic libraries to release their objects. I call libraries shutting down functions in classes destructors which are called after the dynamic libraries were unloaded.
Pseudo code :
// main.d
main()
{
DerelictLua.load(); // Load lua library and affect all lua functions ptr
LuaContext script = new LuaContext();
return script.execute();
}
static ~this()
{
DerelictLua.unload(); // Unload the liblua.so or dll, first
}
// luaContext.d
class LuaContext
{
this()
{
mLuaContext = lua_newstate();
}
~this()
{
lua_close(mLuaContext); // Release the main lua library object, second (lua_close is now a bad pointer)
}
Lua_State* mLuaContext;
}
I am expecting because the "script" variable isn't use out from the "main" function scope having no issue of this kind.
Comment #1 by code — 2013-04-07T16:56:15Z
You must not use any shared library after unloading.
The simplest approach right now is to nullify any class from the shared library and call GC.collect so that they get collected and finalized.
Comment #2 by flamaros.xavier — 2013-04-08T02:50:32Z
(In reply to comment #1)
> You must not use any shared library after unloading.
> The simplest approach right now is to nullify any class from the shared library
> and call GC.collect so that they get collected and finalized.
To fix it I add "delete script" before the "main" exit, but I thought druntime call gc.collect just after my main return.
Maybe I need to do like in java with hiding (or destroy(object)) objects I will not use anymore?
Comment #3 by code — 2013-04-08T05:38:40Z
Yeah destroy finalizes your object and resets it's data to the init state.
You'd have to check for a null mLuaContext in your destructor for this to work
because the GC will finalize your object again.
Comment #4 by code — 2013-04-08T05:40:15Z
> It seems gc is called after the main function to release all allocation, but
only after all static this method were called.
Because the module destructors may reference and use GC allocated memory.