[Issue 1550] New: D DLLs close standard input/output streams when unloading

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Oct 7 14:32:19 PDT 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 at digitalmars.com
        ReportedBy: thecybershadow at 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.


-- 



More information about the Digitalmars-d-bugs mailing list