destructors and GC again

Lutger lutger.blijdestijn at gmail.com
Mon Oct 16 09:57:21 PDT 2006


Sean Kelly wrote:
> Lutger wrote:
>> Sorry for this topic again, I'm still not exactly sure: when an 
>> unreferenced object is collected during the lifetime of your program 
>> (before main exits), is the destructor of this object guaranteed to be 
>> called? I expect it to be so, as otherwise destructors seem pretty 
>> useless (and dangerous), but cannot infer this from the spec.
> 
> When an object is collected by the GC, it's dtor will be called. 
> However, the GC may not detect an orphaned object as collectable if 
> there is a value in memory somewhere that "looks" like a reference to 
> that object.
> 
> 
> Sean

Good, the possibly not collecting part is no problem.

It would make more sense to me if these sentences in the spec were 
different:

Spec: "The garbage collector is not guaranteed to run the destructor for 
all unreferenced objects"
My interpretation: "The garbage collector is not guaranteed to collect 
all unreferenced objects"

Spec: "The garbage collector calls the destructor function when the 
object is deleted."
My interpretation: "The garbage collector calls the destructor function 
when the object is collected."



More information about the Digitalmars-d-learn mailing list