destructors

Daniel Giddings daniel.giddings at gmail.com
Tue May 1 06:34:01 PDT 2007


Bill Baxter wrote:
> Is this ok in D?
> 
> 
> class MyClass
> {
>    OtherClass a;
>    this() {
>       a = new OtherClass;
>    }
>    ~this() {
>       delete a;
>    }
> }
> 
> I was just trying to fix the crash on exit in the tinyXPath code on 
> dsource, and it does the above a lot.
> 
> --bb

I would expect that to work (it is something I would want to be able to 
do - otherwise you would need a finalise method - yuck), I can't recall 
if I have done it though?

a should still be detected as having objects referencing it until the 
MyClass destructor is finished I would think (if the delete a wasn't 
there). Otherwise you would not be able to rely on any class properties 
as still existing in its destructor (ie a.method() could cause an 
exception as well).


More information about the Digitalmars-d-learn mailing list