Destructors and Deterministic Memory Management
Daniel Keep
daniel.keep.lists at gmail.com
Wed May 6 14:58:49 PDT 2009
Georg Wrede wrote:
> ...
>
> I've always found that sentence a bit murky in the docs. Thinking more,
> isn't this what happens:
>
> class A {
> A b = new A;
> }
> void main() { A c = new A;}
>
> When ca gets collected, it is not "guaranteed" that b gets distroyed
> first.
No, it is not guaranteed that c gets destroyed first.
> Fine. But suppose A has a destructor that says delete b. Wouldn't
> that guarantee that b gets destroyed before c?
No. GC does a collect and marks both c and b for collection. It blows
up b. It goes to blow up c and notices it has a dtor. It calls the
dtor. Dtor attempts to delete b. But b is a pointer into a chunk of
memory that no longer exists. b's dtor explodes, killing several nearby
pedestrians and one dog.
When your class' dtor is called, you CANNOT say whether any of the
references into GC-controlled memory you hold are still valid.
-- Daniel
More information about the Digitalmars-d
mailing list