GC: finalization order?!

Martin Kinkelin noone at spam.com
Sat Feb 19 05:54:43 PST 2011


Hi,

I'm very surprised by the GC finalization order (D 2.051, Windows).

Minimalistic test:
----------
import std.stdio;

class Child
{
    this()  { writeln("Child.__ctor()"); }
    ~this() { writeln("Child.__dtor()"); }
}

class Parent
{
    private Child _child;
    this()  { writeln("Parent.__ctor()"); _child = new Child(); }
    ~this() { writeln("Parent.__dtor()"); }
}

int main(string[] args)
{
    auto parent = new Parent();
    return 0;
}
----------

Output:
----------
Parent.__ctor()
Child.__ctor()
Child.__dtor()
Parent.__dtor()
----------

So parent._child gets destructed before parent, although parent
obviously holds a reference to the Child instance. My problem is that
I need to access _child in Parent.__dtor(), which therefore doesn't
work as I expected.
Is this a bug or really intended behaviour?!

Thanks in advance,

Martin


More information about the Digitalmars-d-learn mailing list