Newbie initial comments on D language - destructor usage
Sean Kelly
sean at f4.ca
Mon Jan 28 17:07:35 PST 2008
Edward Diener wrote:
> I have a small nit having to do with a general comment which perhaps I
> did not really understand. In the documentation on class destructors I
> read:
>
> "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 rule does not apply to auto objects or
> objects deleted with the DeleteExpression."
>
> This comment makes perfect sense up to the last sentence. Except for a
> 'scope' class a destructor can not possibly know how it is being called
> so if someone can explain to me how a destructor can know when it is OK
> to reference sub-objects or not, I would love to hear about it.
Under normal circumstances, this is exactly right. Tango has a feature
whereby the finalization method can be hooked and altered however. See
slide 18 of this presentation for an example:
http://s3.amazonaws.com/dconf2007/SeanKelly.ppt
> I would
> have thought that referencing sub-objects in a destructor was just
> forbidden unless the class was a 'scope' class and had to reference a
> sub-object to possibly release a non-memory resource. But perhaps I am
> missing the full meaning of the comment and someone can explicate the
> issues involved for me.
In general I think you're right.
Sean
More information about the Digitalmars-d
mailing list