GC: memory collected but destructors not called

ketmar via Digitalmars-d digitalmars-d at puremagic.com
Mon Nov 10 00:27:52 PST 2014


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. ;-)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20141110/b2965176/attachment.sig>


More information about the Digitalmars-d mailing list