Call to Runtime.unloadLibrary corrupts stdout and stderr

Rainer Schuetze via Digitalmars-d digitalmars-d at puremagic.com
Sun Sep 3 06:41:33 PDT 2017



On 02.09.2017 23:38, Markus Pursche wrote:
> Hi, I'm trying to hot reload a DLL while the program is running to allow 
> for rapid iteration in a game engine I am working on. I started reading 
> up on how DLLs work between D code here: 
> https://wiki.dlang.org/Win32_DLLs_in_D#D_code_calling_D_code_in_DLLs

Unfortunately, this section only applies to simple programs (no 
multi-threading, no sharing of other resources than GC-memory, not even 
malloc'ed memory). I think there should be a big red warning at the 
beginning of that section.

Proper DLL support for Windows (with a shared phobos library) is slowly 
being worked on, see http://dconf.org/2016/talks/thaut.html

In the mean time, I prefer keeping DLL resources completely separate, as 
shown in https://wiki.dlang.org/Win32_DLLs_in_D#DLLs_with_a_C_Interface

> 
> I followed that example, however when I try to do the following I crash:
> 1. Load DLL
> 2. Unload DLL
> 3. Load DLL again
> 
> The problem I am getting is a Bad File Descriptor exception right after 
> step 2, on my very next writeln to be exact.
> 
> Here is the exception I am getting: http://imgur.com/a/b9XBm
> I spent a couple of hours trying to debug it, googling all sorts of 
> combinations of "dlang unloadlibrary bad file descriptor", until I got 
> the idea to try "dlang freelibrary bad file descriptor" and found this 
> old bug report from 2004:
> 
> http://forum.dlang.org/post/cg5hpf$kpv$1@digitaldaemon.com
> 
> This sounds absolutely spot on to the problems I am having. I tried to 
> save stdout and reset it after unloading my library but I still crashed 
> on the next writeln, does anyone have an idea for a workaround on this?
> 
> I'm hoping that someone like Walter or Andrei will see this and decide 
> that it's time for this 2004 issue to finally die. :P

You are probably hitting https://issues.dlang.org/show_bug.cgi?id=1550, 
so try the workaround: import std.stdio and add "_fcloseallp = null;"
somewhere in your DllMain.


More information about the Digitalmars-d mailing list