Issue with module destructor order

Benjamin Thaut code at benjamin-thaut.de
Mon Mar 26 01:55:47 PDT 2012


Hi,
I came across a issue with the order in which druntime calls the module 
destructors. I created a small repro case for it:

file a.d:
module a;

import logger;

shared static this()
{
   log("a init");
}

shared static ~this()
{
   log("a deinit");
}

file b.d:
module b;

import mix;

class Foo
{
   mixin logIt;
}

file logger.d:
module logger;

import std.stdio;

shared static this()
{
   writefln("logger init");
}

void log(string message)
{
   writefln(message);
}

shared static ~this()
{
   writefln("logger deinit");
}

file main.d:
module main;


import std.stdio;

import a;

import b;



int main(string[] argv)
{
   auto foo = new Foo();

   writefln("main");

   return 0;

}


mix.d:
module mix;

public import logger;

mixin template logIt()
{
   static shared this()
   {
     log(typeof(this).stringof ~ " init");
   }

   static shared ~this()
   {
     log(typeof(this).stringof ~ " deinit");
   }
}

Compile with: dmd a.d b.d logger.d main.d mix.d -oftest.exe
Running gives the following output:

logger init
a init
shared(Foo) init
main
a deinit
logger deinit
shared(Foo) deinit

That means that the logger module destructor is called before the b 
module actually gets destructed which leads to the logger beeing used in 
a destructed state. In my real world case this leads to a fiel operation 
on a closed file handle. Is this intended behaviour or is this a bug? I 
assume this happens because of the mixin template and the public import.
I'm using dmd 2.058.

-- 
Kind Regards
Benjamin Thaut


More information about the Digitalmars-d mailing list