GC does not delete subclass

Jason House jason.james.house at gmail.com
Tue Dec 18 10:52:03 PST 2007


Steven Schveighoffer Wrote:

> "Jason House" wrote
> > I haven't tried this, but is it possible to declare class member variables 
> > as scope so that they will get deleted when the rest of the object does? 
> > This could save coding of custom destructors in some cases.
> 
> Custom destructors should NEVER destroy other GC allocated objects.  the GC 
> cannot guarantee the order of collection, meaning if you have a pointer to 
> another GC allocated object, that object may have already been collected.
> 
> There is no real reason to delete other objects in a destructor.  If the 
> objects are no longer referenced, the GC will destroy those also.  The only 
> things you should destroy in a destructor are non-GC allocated resources, 
> such as file descriptors, or memory allocated with C's malloc.
> 
> So to answer your question: scope is not necessary as a modifier for a 
> member variable.

I wasn't asking about necessary.  I was asking if it's valid.  It's easy enough to construct RAII-like examples where a timely destruction of a member variable would be handy.  It can also be handy when standard GC operation is overridden.



More information about the Digitalmars-d mailing list