Destructor order

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Oct 22 10:13:34 PDT 2014


On Wednesday, 22 October 2014 at 16:55:41 UTC, Regan Heath wrote:
> "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."

But Scoped!A is on the stack?

So why wasn't the eles' destructor order in reverse if Scoped is 
a struct and calls explicit destroy(B) then destroy(A)?

https://github.com/D-Programming-Language/phobos/blob/master/std/typecons.d#L4628

         ~this()
         {
             .destroy(Scoped_payload);
         }


More information about the Digitalmars-d-learn mailing list