February 18, 2015 Structs on the heap and destructors. | ||||
|---|---|---|---|---|
| ||||
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?
| ||||
February 18, 2015 Re: Structs on the heap and destructors. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to John Colvin | 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.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply