digitalmars.D.learn - Structs on the heap and destructors.
- John Colvin (31/31) Feb 18 2015 using DMD git HEAD
- Adam D. Ruppe (8/9) Feb 18 2015 That'll never happen; B is still valid until after A is
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?
Feb 18 2015
On Wednesday, 18 February 2015 at 15:22:47 UTC, John Colvin wrote:doOtherStuff must be called before doStuff.That'll never happen; B is still valid until after A is destroyed, if they weren't on the heap you'd see doStuff goes first too. You should probably make a method to clean B up yourself, I wouldn't even use the destructor, just a regular like "dispose" method that you can guard with a flag. I think that's the best way to do it.
Feb 18 2015








"Adam D. Ruppe" <destructionator gmail.com>