www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9900] New: static this and gc shutdown order issue

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9900

           Summary: static this and gc shutdown order issue
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: druntime
        AssignedTo: nobody puremagic.com
        ReportedBy: flamaros.xavier gmail.com


--- Comment #0 from Xavier Bigand <flamaros.xavier gmail.com> 2013-04-07
15:17:16 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 07 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9900


Martin Nowak <code dawg.eu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |code dawg.eu


--- Comment #1 from Martin Nowak <code dawg.eu> 2013-04-07 16:56:15 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 07 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9900



--- Comment #2 from Xavier Bigand <flamaros.xavier gmail.com> 2013-04-08
02:50:32 PDT ---
(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? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 08 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9900



--- Comment #3 from Martin Nowak <code dawg.eu> 2013-04-08 05:38:40 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 08 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9900


Martin Nowak <code dawg.eu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |WONTFIX


--- Comment #4 from Martin Nowak <code dawg.eu> 2013-04-08 05:40:15 PDT ---
 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. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 08 2013