Destruction Sequence: module and classes defined within

Lars T. Kyllingstad public at kyllingen.NOSPAMnet
Wed Oct 6 00:50:07 PDT 2010


On Tue, 05 Oct 2010 23:25:36 +0200, vano wrote:

> The code below:
>      module used;
> 
>      import std.stdio;
> 
>      class ClassA {
>          this()  { writeln("A ctor"); }
>          ~this() { writeln("A dtor"); }
>      }
> 
>      static this()  { writeln("used.sctor"); } static ~this() {
>      writeln("used.sdtor"); }
> 
>      void main() {
>          auto a = new ClassA();
>      }
> produces the following output (DMD v2.049):
>      used.sctor
>      A ctor
>      used.sdtor
>      A dtor
> 
> The question is: should the module be allowed to be unloaded before all
> module-level objects/structures are destructed/unloaded?


I'm no expert on this, but I think it has to be that way.  Consider this:

  class Foo { ... }
  Foo foo;

  static this()
  {
      foo = new Foo;
  }

  static ~this()
  {
      foo.doStuff();
  }

So you see, if foo had already been destroyed and garbage collected, my 
program would have crashed when the module static destructor was run.  
Thus, I guess, running the garbage collector for the final time has to be 
one of the last things done on program shutdown, after running all module 
destructors.

-Lars


More information about the Digitalmars-d-learn mailing list