GC: memory collected but destructors not called

via Digitalmars-d digitalmars-d at puremagic.com
Wed Nov 12 06:20:07 PST 2014


On Wednesday, 12 November 2014 at 13:56:08 UTC, Shachar Shemesh 
wrote:
> On 12/11/14 11:29, "Marc =?UTF-8?B?U2Now7x0eiI=?= 
> <schuetzm at gmx.net>" wrote:
>
>> Supposedly, a struct destructor will only access resources 
>> that the
>> struct itself manages. As long as that's the case, it will be 
>> safe. In
>> practice, there's still a lot that can go wrong.
>
> Either a struct's destructor can be run from the context of a 
> GC, in which case it should run when the struct is directly 
> allocated on the heap, or it is not, in which case the fact it 
> is run when the struct is inside a class should be considered a 
> bug.
>
>
> Today it happens for structs nested in classes, but not 
> allocated directly. I don't see any situation in which this is 
> not a bug.
>
> Shachar

I think it's helpful to ask the question who's responsible for 
destroying an object. If it's the GC, then it's finalization, if 
it's no the GC, it's destruction. Both a destructor and a 
finalizer need to clean up themselves, and any other object they 
own. This includes embedded structs, but not GC managed objects 
created by the constructor.

This applies to both structs and classes as the owning objects, 
and manual/automatic management as well as GC.

But indeed, what's implemented today is inconsistent.


More information about the Digitalmars-d mailing list