Reddit: SafeD - The Safe Subset of D

Chris Miller lordSaurontheGreat at gmail.com
Tue Mar 25 23:29:19 PDT 2008


Chris Miller Wrote:
> 
> http://www.digitalmars.com/d/2.0/class.html#Destructor
> 
> "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 are no longer valid. This means that destructors cannot reference sub objects. This is because that the garbage collector does not collect objects in any guaranteed order, so there is no guarantee that any pointers or references to any other garbage collected objects exist when the garbage collector runs the destructor for an object."

Oh, well then you should just move the code you wanted to run in the parent object's destructor to the sub-object's destructor, though it does leave my wondering why the garbage collector would have killed the sub-object first, since if there is code in the parent object's destructor that uses the sub-object, that should count as a reference, so the sub-object should still be in scope - I think.  Or does code in a destructor not count towards keeping heap references in scope?

-- the "other" Chris Miller



More information about the Digitalmars-d-announce mailing list