Thread overview
[Issue 1550] New: D DLLs close standard input/output streams when unloading
Oct 07, 2007
d-bugmail
Oct 07, 2007
d-bugmail
Apr 07, 2010
Rainer Schuetze
Oct 06, 2010
SHOO
October 07, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1550

           Summary: D DLLs close standard input/output streams when
                    unloading
           Product: D
           Version: 1.022
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P3
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: thecybershadow@gmail.com


=== loader.d ===

import std.stdio;
import std.string;
import std.c.windows.windows;

void main()
{
        writefln("Loading DLL...");
        HANDLE h = LoadLibraryA("library.dll");
        if(!h)
                return writefln("Failed to load DLL.");
        writefln("DLL loaded. Unloading...");
        if(!FreeLibrary(h))
                return writefln("Failed to unload DLL.");
        writefln("DLL unloaded.");
}

=== library.d ===
(standard DLL template copied from the documentation)

import std.c.windows.windows;
HINSTANCE g_hInst;

extern (C)
{
        void gc_init();
        void gc_term();
        void _minit();
        void _moduleCtor();
        void _moduleUnitTests();
}

extern (Windows)
BOOL DllMain(HINSTANCE hInstance, ULONG ulReason, LPVOID pvReserved)
{
    switch (ulReason)
    {
        case DLL_PROCESS_ATTACH:
            gc_init();                  // initialize GC
            _minit();                   // initialize module list
            _moduleCtor();              // run module constructors
            _moduleUnitTests();         // run module unit tests
            break;

        case DLL_PROCESS_DETACH:
            gc_term();                  // shut down GC
            break;

        case DLL_THREAD_ATTACH:
        case DLL_THREAD_DETACH:
            // Multiple threads not supported yet
            return false;
    }
    g_hInst=hInstance;
    return true;
}

=== output ===
Loading DLL...
DLL loaded. Unloading...

=== comments ===
D's runtime (DMC libc) closes the standard streams when the DLL is unloaded.
Call stack:

DllEntryPoint
__cexit
_exit
___fcloseall
_fclose
_close

Needless to say, that shouldn't happen with DLLs.

Workaround: import std.stdio and add
            _fcloseallp = null;
somewhere in your DllMain.


-- 

October 07, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1550





------- Comment #1 from thecybershadow@gmail.com  2007-10-07 17:04 -------
I just noticed that this is somewhat documented at
http://digitalmars.com/d/dll.html#Dcode .
However, this applies not only when a program in D is the one loading the DLL.
Thus, it should be moved outside the "D code calling D code in DLLs" section,
and also into the "DLLs with a C Interface" source code template.


-- 

April 07, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=1550


Rainer Schuetze <r.sagitario@gmx.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |r.sagitario@gmx.de


--- Comment #2 from Rainer Schuetze <r.sagitario@gmx.de> 2010-04-07 11:20:55 PDT ---
The workaround (setting _fcloseallp = null) also prevents any other open file
from being closed.

I think it should be fixed in the C-runtime library, where the handle for stdin/out/err is taken from a call to GetStdHandle(), and probably should not be closed when terminating.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 06, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=1550


SHOO <zan77137@nifty.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |zan77137@nifty.com


--- Comment #3 from SHOO <zan77137@nifty.com> 2010-10-06 05:50:52 PDT ---
*** Issue 4996 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------