GC: finalization order?!

Jonathan M Davis jmdavisProg at gmx.com
Sun Feb 20 04:35:35 PST 2011


On Sunday 20 February 2011 04:10:18 Simen kjaeraas wrote:
> Jonathan M Davis <jmdavisProg at gmx.com> wrote:
> > Yes. You're right. They can reference the non-GC heap just fine. It's
> > just that
> > they can't reference the GC heap - probably because the destructor order
> > is
> > indeterminate and so that the GC doesn't have to worry about dealing with
> > circular references between garbage collected objects.
> 
> D could support finalizers in lieu of (or in addition to) destructors. In
> such a case, they would be called before the object graph were garbage-
> collected, and one could hence reference other objects on the GC heap. Is
> there any reason why this approach was not chosen?

I'm really not very well versed in the details of the GC, and I don't know all 
that much about why it works the way that it works. For the most part, though, I 
don't see much need for either destructors or finalizers in classes. The main 
reason for them in C++ is for managing memory, which the GC takes care of for 
you. And since RAII is taken care of by structs, there really isn't much reason 
for class destructors that I can see - at least not normally. I can see why you 
might need it occasionally, but for the most part, you don't. Java has finalizers 
(but no constructors), but I've never needed them, and I've never seen them 
used. So, while the situation with class destructors and finalizers could be 
handled better in D, I don't think that it's normally an issue at all.

Regardless, I'm not particularly well versed in the details of the GC, so I'm 
really not the best person to answer questions about it.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list