Destructor semantics

Rainer Deyke rainerd at eldwood.com
Tue Aug 10 18:23:53 PDT 2010


On 8/10/2010 16:59, foobar wrote:
> Steven Schveighoffer Wrote:
>> what happens when GC destroys a C?
>> 
>> C::~this(); // auto generated
>>   B::~this(); // so good so far 
>>     A::~this(); // oops!  the a is gone, program vomits bits all over itself and
>> chokes to death.
>> 
>> -Steve
> 
> This can only happen if you use delete on a class instance. My
> understanding was that this is going to be removed from D2.

Same problem without 'delete':

class A {
  void dispose();
}

struct B {
  A a;
  ~this() { a.dispose(); }
}

class C {
  B b;
}

C::~this(); // auto generated
  B::~this(); // so good so far
    A::dispose(); // oops!


-- 
Rainer Deyke - rainerd at eldwood.com


More information about the Digitalmars-d mailing list