Destructor order

Regan Heath via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Oct 22 09:55:40 PDT 2014


On Wed, 22 Oct 2014 16:49:20 +0100, eles <eles at eles.com> wrote:

> On Wednesday, 22 October 2014 at 15:45:02 UTC, eles wrote:
>
> D version with structs:
>
> 	{ //display ~C~B~A
> 	    A foo;
> 	    B bar;
> 	    C *caz = new C();
> 	    delete caz;
> 	}
>
> as expected.

Structs are special, compare:
http://dlang.org/struct.html#struct-destructor

with:
http://dlang.org/class.html#destructors

Specifically:
"The garbage collector is not guaranteed to run the destructor for all  
unreferenced objects. Furthermore, the order in which the garbage  
collector calls destructors for unreference objects is not specified. This  
means that when the garbage collector calls a destructor for an object of  
a class that has members that are references to garbage collected objects,  
those references may no longer be valid. This means that destructors  
cannot reference sub objects. This rule does not apply to auto objects or  
objects deleted with the DeleteExpression, as the destructor is not being  
run by the garbage collector, meaning all references are valid."

Regan

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/


More information about the Digitalmars-d-learn mailing list