Structs on the heap and destructors.

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Feb 18 07:22:46 PST 2015


using DMD git HEAD

struct A
{
     B* b;

     ~this()
     {
         doStuff();
     }
}

struct B
{
     ~this()
     {
         doOtherStuff();
     }
}

example usage:

void main()
{
     A a;
     a.b = new B;
}


Requirements:

doOtherStuff must be called before doStuff.
doOtherStuff and doStuff must be called exactly once each.

Possible solution:

insert a b.__dtor(); before doStuff. Unfortunately b.__dtor() is 
called again on exit (by the GC I assume). doOtherStuff can be 
guarded with a flag to prevent this.

b.destroy() doesn't seem to actually call B.__dtor().


Is there a "right" way to do this?


More information about the Digitalmars-d-learn mailing list