class destructors must be @disabled?

Steven Schveighoffer schveiguy at gmail.com
Wed May 18 19:45:59 UTC 2022


On 5/18/22 2:58 PM, H. S. Teoh wrote:
> On Wed, May 18, 2022 at 02:35:00PM -0400, Steven Schveighoffer via Digitalmars-d-learn wrote:
> [...]
>> No. Class destructors are for cleaning up non-GC resources. As long as
>> you stick to those, you can safely run them.
>>
>> Structs that get put into classes have to run their destructors
>> properly, otherwise, you will have horrible inconsistencies.
>>
>> For instance, I would not want to disable the destruction of a
>> RefCounted struct inside a class.
> [...]
> 
> So if the user runs your program with --DRT-gcopt=cleanup:none and you
> happen to have a RefCounted struct inside a GC-allocated class, then
> you're screwed?

I approach it from a different way. Let's say I'm writing a File class, 
and it has a file descriptor inside it.

It's not me that's deciding when to clean up the object. All I want to 
do as the *library author* is to clean up the resource *I* opened, when 
someone is cleaning my object up. In other words, I don't care how you 
destroy it, GC, synchronously, etc, when you clean up the class 
instance, I will clean up my mess. That's just good lifetime management.

Not cleaning it up because you're afraid of destructors is not the answer.

That being said, I think it's a reasonable position for you to not 
support running your program with that DRT option (obviously, we do rely 
on the GC to clean up some things).

-Steve


More information about the Digitalmars-d-learn mailing list