A few considerations on garbage collection

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Wed Apr 30 17:08:03 PDT 2014


On Wed, 30 Apr 2014 14:15:03 -0400, Dmitry Olshansky  
<dmitry.olsh at gmail.com> wrote:

> 30-Apr-2014 19:33, Andrei Alexandrescu пишет:

>> 4. Currently, struct objects created with new do NOT have their
>> destructors called during collection. I think this may continue, meaning
>> that structs created with new are somewhat low-level and are meant to be
>> destroyed and deallocated manually.

I think structs can and will be destructed from the heap at some point.  
What we do need, however, is some more intelligence in GC destruction.  
Assumptions don't cut it, especially in multi-threaded code.

> IIRC they do, it's only arrays of such that doesn't. Anyhow having such  
> a dangerous construct built-in (new = resource leak) in the language  
> becomes questionable.

No, they don't. Only objects are marked as having a finalizer. The  
finalize flag in the GC assumes that the first size_t in the block is a  
pointer to a vtable. A struct cannot have this.

We need to fundamentally modify how this works if we want finalizers for  
structs to be called, but I think it's worth doing. IIRC, Rainer's precise  
GC does this.

>> 5. This brings up arrays of structs. As far as I understand, destructors
>> will never be called for these, even after all references are gone:
>>
>> struct S { ~this() { ... } }
>> auto a = new S[100];
>>
>> Unlike (4), arrays of structs are high-level and frequently used. I
>> think we must do something about it, so I plan to support calling
>> destructors for arrays of structs.

An array of structs is a struct itself, and not a class. While it could  
conceivably be done much easier than structs with the current layout, it  
would eliminate 16-byte arrays, because the first size_t would have to be  
reserved! Hopefully my explanation above helps to understand this.

> Making the point about NOT calling destructor that much more  
> schizophrenic. Either do it properly or not at all.

Agree.

-Steve


More information about the Digitalmars-d mailing list