Destructors vs. Finalizers
Steven Schveighoffer via Digitalmars-d
digitalmars-d at puremagic.com
Thu Jul 27 04:43:37 PDT 2017
On 7/27/17 4:23 AM, Guillaume Piolat wrote:
> On Wednesday, 26 July 2017 at 22:29:50 UTC, Steven Schveighoffer wrote:
>> Today, the finalizer is essentially a last-effort to clean up
>> resources that would otherwise leak.
>
> My point is that this "last chance" to clean up resources should only be
> used to tell deterministic destruction wasn't acheived (for reasons
> given in my former post: it's _too hard to explain_ how to do resource
> management else).
Consider reference counting with cycles. The proposal from Walter/Andrei
is to do reference counting to clean up everything but cycles. For
cycles, the GC will take care of it.
So how do you do this correctly without having two separate mechanisms?
>> The reason deterministic destruction sucks today is because once you
>> go 2 levels deep, the only tool available (~this) cannot release any
>> resources (because it's not legal to access GC-allocated members
>> inside ~this).
>
> You already know it, but if you commit to not doing anything in the
> finalizer case, then ~this doesn't suck, it works as a destructor and
> the restrictions lift.
> So "suck" is a bit strong.
This is an unworkable solution. One simple time you forget to clean up
deterministically and then you corrupt memory by using members that are
already cleaned up.
Or, you disable calling destructors in the GC, and instead leak resources.
Or I suppose you could crash your application. This would be a fun bug
to figure out...
-Steve
More information about the Digitalmars-d
mailing list