GC: finalization order?!

bearophile bearophileHUGS at lycos.com
Sat Feb 19 05:59:48 PST 2011


Martin Kinkelin:

> So parent._child gets destructed before parent, although parent
> obviously holds a reference to the Child instance. My problem is that
> I need to access _child in Parent.__dtor(), which therefore doesn't
> work as I expected.
> Is this a bug or really intended behaviour?!

It's intended, despite being not nice for the programmer. Generally in D finalization order done by the GC is not deterministic (it's not even sure you will have finalizations, I think), so you must design your program in a different way (like using RAII and structs, etc). Python GC is based on enhanced reference counting, so it's deterministic. But D uses a less deterministic GC, like a mark & sweep.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list