Finalizers, Destructors, RAII, GC
sighoya
sighoya at gmail.com
Wed Apr 28 22:05:31 UTC 2021
On Wednesday, 28 April 2021 at 10:04:20 UTC, Ola Fosheim Grøstad
wrote:
> 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.
Doesn't suffice ownership here? I mean you surely talk about
owned manually allocated memory inside a class.
But why the need at all for a finalizer and instead allow
non-empty destructors for gc allocated classes?
Having both is confusing.
> 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?
Probably not for gc managed resources, but what about making
class members reference counted or owned instead. Owned resources
should be deleted automatically when a class object is destructed
and reference counted resources are deleted if refcount goes to
zero, which may occurs after destruction but at least not before.
Btw, I don't know how much is currently possible in D.
More information about the Digitalmars-d
mailing list