Finalizers, Destructors, RAII, GC

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Wed Apr 28 10:04:20 UTC 2021


It is currently an issue that people might write a class for RAII 
and then allocate in on the GC heap, that means that destructors 
can be called in the wrong order.

If D adds a finalizer() method, this could be resolved the 
following way:

Objects with non-empty destructors cannot be GC allocated.

The finalizer() method should be used for releasing all non-GC 
resources accessible from non-GC pointers/identifiers, but one 
should not follow pointers to GC-memory from a finalizer as they 
may not exist.

So a RAII object needs to call the finalizer from the destructor, 
but exactly what would the implications be for inheritance?

Anyway, if it isn't clear, the basic goal is to allow RC-based 
RAII and GC to coexist.

Or would it be better to simply forbid any cleanup in the GC? I 
kinda think so, but I also think many D users are used to letting 
the GC trigger cleanup on collection.

What do you think?



More information about the Digitalmars-d mailing list