Destructor nonsense on dlang.org

Jacob Carlborg doob at me.com
Fri May 25 04:23:39 PDT 2012


On 2012-05-25 11:02, foobar wrote:

> It makes the call order deterministic like in C++.
>
> e.g.
> class Foo {}
>
> struct A {
> resource r;
> ~this() { release(r); }
> }
>
> struct B {
> A* a;
> Foo foo;
> ~this() { delete a; } // [1]
> }

I though we were talking about classes holding structs:

class B {
     A* a;
     Foo foo;
     ~this() { delete a; }
}

In this case you don't know when/if the destructor of B is called. It 
doesn't help to wrap it in a struct, you could just have put it directly 
in A. Is that correct?

> Lets look at point [1]:
> The "foo" instance is "managed" by the GC since the only resource it
> holds is memory. The "a" member wraps some "non-managed" resource (e.g.
> file descriptor) and in this model is still valid, thus allows me to
> deterministically dispose of it as in c++.

Ok, but if B is a class?

> This can be simply checked at compile-time - you can only reference non
> class instance members in the destructor, so adding a "delete foo;"
> statement at point [1] simply won't compile.

If you have a pointer to a struct you don't know how it was created. 
It's possible it's been created with "new", which means the garbage 
collector needs to delete it.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list