Destructor called while object is still alive

Guillaume Piolat first.name at guess.com
Sat Oct 24 12:01:34 UTC 2020


On Friday, 23 October 2020 at 07:32:02 UTC, Ola Fosheim Grøstad 
wrote:
>
> So basically, destructors do not play well with GC. I've in the 
> past argued that one should not allow destructors on GC objects 
> since there are no proper guarantees anyway. That could also 
> speed up collection. But people prefer convenience over 
> correctness...
>

100% agree.

I think Andrei also argued once that class destructors should not 
be called by the GC.
But what happened instead later on is that GC-allocated struct 
also did get their destructor called by the GC.

Not that the very-much-unused "GC Proof Resource Class"[1] idiom 
turns the GC into a detector for... overreliance on the GC for 
freeing resources.

[1] http://p0nce.github.io/d-idioms/#GC-proof-resource-class


> IMO, rule of thumb: do not rely on destructors being called at 
> the appropriate time for objects on the GC heap. Do explicit 
> finalisation instead.

For the GC objects that transitively own something that isn't 
memory.

If A owns B owns (a GC-allocated int[]),
then A and B destructors don't need to be called manually.



More information about the Digitalmars-d mailing list