On Thursday, 24 May 2012 at 17:55:02 UTC, Alex Rønne Petersen wrote:
I would strongly advise against that, because a missed clear() means your finalizer may be run by the runtime's finalization machinery, and thus invalidate any invariants you were relying on in the finalizer.

Yes – the »correct« way to handle situations where you need deterministic finalization is to use structs on the stack, possibly in conjunction with reference counting. Of course, there are some situations (e.g. when there are cycles) where this doesn't work, but at least it covers most of the »external non-memory resource« cases.

Composability can still be a problem, though, because holding a reference in a (GC-managed) class object might leave you with exactly the same problem you tried to avoid in the first place.

David

Reply via email to