GC: memory collected but destructors not called

Tomer Filiba via Digitalmars-d digitalmars-d at puremagic.com
Mon Nov 10 01:13:09 PST 2014


Thanks for the info, ketmar.

By the way, what do you guys think of adding @nogc to structs, in 
which case they cannot be instantiated by the GC?

e.g.,

@nogc struct S {...}
auto s = new S(); // does not compile
S s;              // all is well


-tomer

On Monday, 10 November 2014 at 08:28:03 UTC, ketmar via 
Digitalmars-d wrote:
> On Mon, 10 Nov 2014 08:10:08 +0000
> Tomer Filiba via Digitalmars-d <digitalmars-d at puremagic.com> 
> wrote:
>
>> The following code does not invoke S.~this. If I change 
>> `struct S` to `class S` - it does. Memory consumption remains 
>> constant, meaning memory is collected, but destructors are not 
>> called.
>> 
>> import std.stdio;
>> import std.conv;
>> 
>> struct S {
>>      string s;
>> 
>>      ~this() {
>>          writeln("~S");
>>      }
>> }
>> 
>> void main() {
>>      auto i = 0;
>>      S[] arr;
>> 
>>      while (true) {
>>          arr ~= S("hello " ~ text(i++));
>>          if (arr.length == 1_000_000) {
>>              writeln(&arr[8888], " = ", arr[8888].s);
>>              arr.length = 0;
>>          }
>>      }
>> }
>> 
>> 
>> Is it a bug? How can I effectively implement RAII with this 
>> behavior?
> it's a bug, it was recently filled and Walter (afair) made a PR 
> with
> fix, but it's not yet merged.
>
>> Any workaround for this?
> made your own array implementation which manually calls dtors. 
> or wait
> for next DMD release with this bug fixed. ;-)



More information about the Digitalmars-d mailing list