class destructors and sub-objects

Ali Çehreli acehreli at yahoo.com
Thu Aug 29 19:54:38 PDT 2013


On 08/29/2013 06:58 AM, d coder wrote:> Greetings
 >
 > I have a question on class destructor method. D documentation for
 > destructors says:
 >
 > "The garbage col­lec­tor is not guar­an­teed to run the de­struc­tor for
 > all un­ref­er­enced ob­jects. Fur­ther­more, the order in which the 
garbage
 > col­lec­tor calls de­struc­tors for un­ref­er­ence ob­jects is not
 > spec­i­fied. This means that when the garbage col­lec­tor calls a
 > de­struc­tor for an ob­ject of a class that has mem­bers that are
 > ref­er­ences to garbage col­lected ob­jects, those ref­er­ences may no
 > longer be valid. This means that de­struc­tors can­not ref­er­ence sub
 > ob­jects."
 >
 >
 > Now I have a class A and it refers to subobject B. But B may or may not
 > have other objects referring to it. As a result, an object of A is GCed,
 > but subobject B may or may not be getting garbage collected.
 >
 > In this scenario, am I allowed to make a reference to subobject B 
from the
 > destructor of A?

The way I read it, it is exactly what the documentation warns against. 
No, A's destructor cannot reference B member.

 > Is there a way to find out if B is garbage collected or
 > not?

Not on B member itself because it is no more. :) It is conceivable to 
maintain a bitmap to determine whether an object is still alive. The 
object's destructor can flip its bit to 0.

 >
 > Regards
 > - Puneet
 >

Ali



More information about the Digitalmars-d-learn mailing list