www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - What is __dtor() supposed to do?

reply Cristi Cobzarenco <cristi.cobzarenco gmail.com> writes:
This is somewhat in reference to a bug that I was having trouble with
(Issue 5667 - http://d.puremagic.com/issues/show_bug.cgi?id=5667).
Right now __dtor() simply represents the function defined between the
curly braces of ~this(), not the compiler generated function that
calls ~this() and the destructors of members in proper order. To
illustrate:

import std.stdio;

struct A { ~this() { writeln( "A.~this()"); } }
struct B {
   ~this() { writeln("B.~this()"); }

   A member;
}

struct C {
   A member;
}

void main() {
     A a;
     B b;
     C c;

     writeln( "using __dtor" );
     a.__dtor(); // prints "A.~this()" as expected
     b.__dtor(); // prints "B.~this()", but not "A.~this()
     // c.__dtor(); // does not compile because there is no ~this() defined in C

     writeln( "using TypeInfo_Struct.destroy" )
     typeid(B).destroy(&a); // prints both A.~this() and B.~this()
     typeid(C).destroy(&c); // compiles & prints A.~this();

     writeln( "going out of scope" );
}

Basically my question is - is this the required behaviour? Or should
__dtor and destroy() do the same thing? If they should, then the bug I
mentioned is a compiler bug. Otherwise, the fix I proposed to druntime
would work.

---
Cristi Cobzarenco
BSc in Artificial Intelligence and Computer Science
University of Edinburgh
Profile: http://www.google.com/profiles/cristi.cobzarenco
Jul 19 2011
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 19 Jul 2011 09:42:32 -0400, Cristi Cobzarenco  
<cristi.cobzarenco gmail.com> wrote:

 This is somewhat in reference to a bug that I was having trouble with
 (Issue 5667 - http://d.puremagic.com/issues/show_bug.cgi?id=5667).
 Right now __dtor() simply represents the function defined between the
 curly braces of ~this(), not the compiler generated function that
 calls ~this() and the destructors of members in proper order. To
 illustrate:

 import std.stdio;

 struct A { ~this() { writeln( "A.~this()"); } }
 struct B {
    ~this() { writeln("B.~this()"); }

    A member;
 }

 struct C {
    A member;
 }

 void main() {
      A a;
      B b;
      C c;

      writeln( "using __dtor" );
      a.__dtor(); // prints "A.~this()" as expected
      b.__dtor(); // prints "B.~this()", but not "A.~this()
      // c.__dtor(); // does not compile because there is no ~this()  
 defined in C

      writeln( "using TypeInfo_Struct.destroy" )
      typeid(B).destroy(&a); // prints both A.~this() and B.~this()
      typeid(C).destroy(&c); // compiles & prints A.~this();

      writeln( "going out of scope" );
 }

 Basically my question is - is this the required behaviour? Or should
 __dtor and destroy() do the same thing? If they should, then the bug I
 mentioned is a compiler bug. Otherwise, the fix I proposed to druntime
 would work.
destroy calls the xdtor member made by the compiler. The code for this function as far as I can tell is generated by the compiler, so I would say this is *definitely* a compiler bug. IMO, __dtor should map to the same thing. There is no reason I can think of to separate out the __dtor and the calls to the destructors for any sub members. Can you? -Steve
Jul 19 2011