I hate class destructors with a burning passion

Mike Parker aldacron at gmail.com
Mon Jun 7 10:51:02 UTC 2021


On Monday, 7 June 2021 at 10:04:32 UTC, Imperatorn wrote:
> On Sunday, 6 June 2021 at 18:55:47 UTC, Guillaume Piolat wrote:
>>>
>> Two first issues can be avoided by not letting the GC call 
>> class destructors, and call them deterministically instead.
>
> Do we have an issue or two for that? Sounds like an 
> enhancement. Should/would it be configurable? Like 
> -gcClassDtors=yes/no/auto kind of a thing or just kill it.

`GC.inFinalizer` was added specifically to address this issue.

```D
if(GC.inFinalizer) {
   // no GC or deterministic stuff allowed here
}
else {
   // do whatever you want
}
```

So if you need GC or deterministic stuff in a class destructor, 
you can invoke it manually and still let the GC worry about 
cleaning up the memory.

The alternatives (adding a separate finalizer, or preventing the 
GC from calling destructors) are massively breaking changes, so I 
don't see that happening. Even had destruction and finalization 
been separate from the beginning, I'm sure people would have 
misused the finalizer anyway just as they did in older versions 
of Java.


More information about the Digitalmars-d mailing list