www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - auto, delete & catch

reply "Ilya Zaitseff" <sark7 mail333.com> writes:
Following code outputs very strange results :)

class A
{
   this() { printf("A "); }
   ~this() { printf("~A "); throw new Exception("E"); }
}

void main()
{
   try
   {
     auto A a = new A();
     delete a; // (1)
   }
   catch (Exception o)
   {
     printf("%.*s ", o.toString());
   }
}

After run, program outputs: "A ~A E ~A", i.e. A dtor is called twice.
If uncomment (1), output is "A ~A ~A ~A ... ~A", i.e. A dtor is called  
many-many times :)
Aug 05 2004
parent "Thomas Kuehne" <eisvogel users.sourceforge.net> writes:
Ilya Zaitseff news:opscahl7ddaaezs2 ilya.tec.amursk.ru...
 Following code outputs very strange results :)

 class A
 {
    this() { printf("A "); }
    ~this() { printf("~A "); throw new Exception("E"); }
 }

 void main()
 {
    try
    {
      auto A a = new A();
      delete a; // (1)
    }
    catch (Exception o)
    {
      printf("%.*s ", o.toString());
    }
 }

 After run, program outputs: "A ~A E ~A", i.e. A dtor is called twice.
 If uncomment (1), output is "A ~A ~A ~A ... ~A", i.e. A dtor is called
 many-many times :)

Under Linux the output is "A ~A E ~A" and "A ~A ~A Error: E" if (1) is uncommented. Note the strange difference between "E" and "Error: E" !
Aug 06 2004