Here is another example:

import std.stdio;
import Scope;

class Foo
{
   this(int i, int j, int k) {
       writeln("Foo.this(): ", i, j, k);
   }
~this() {
       writeln("Foo.~this()");
   }
}

class Bar
{
   this(int i, int j, int k)
   {
       writeln("Bar.this(): ", i, j, k);
       foo = Scope!(Foo)(i, j, k);
   }
~this()
   {
       writeln("Bar.~this()");
   }

   Scope!(Foo) foo;
}

// Just for comparison - note that they are almost the same
class NoScopeBar
{
   this(int i, int j, int k)
   {
       writeln("NoScopeBar.this()");
       foo = new Foo(i, j, k);
   }

   ~this()
   {
       writeln("NoScopeBar.~this()");
   }

   Foo foo;
}

void main()
{
   auto bar = Scope!(Bar)(1, 2, 3);
}

Expected output:

Bar.this(): 123
Foo.this(): 123
Bar.~this()
Foo.~this()

Output:
Bar.this(): 123
Foo.this(): 123
Bar.~this()

It is a DMD bug: calling an object's dtor should call dtors on all its fields.

Reply via email to