[Issue 704] destructors are called even if the constructor throws an exception

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed May 9 15:42:29 PDT 2007


http://d.puremagic.com/issues/show_bug.cgi?id=704


braddr at puremagic.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |major




------- Comment #1 from braddr at puremagic.com  2007-05-09 17:42 -------
Retesting with a newest version of the compiler (1.014), these two tests should
probably be altered with s/auto/scope/ due to the changes in the language.

Also, throwing from a destructor is iffy behavior by itself.  Either way, they
still hit the throw in the dtor.

However, fixing the throw from destructor issue like this:
     1  module dstress.run.a.auto_14_A;
     2  
     3  import std.stdio;
     4  
     5  bool dtorcalled = false;
     6  
     7  auto class MyClass{
     8      this(){
     9          writefln("in ctor");
    10          throw new Exception("dummy");
    11      }
    12  
    13      ~this(){
    14          writefln("in dtor");
    15          dtorcalled = true;
    16      }
    17  }
    18  
    19  int main(){
    20      try{
    21          auto MyClass c;
    22          c = new MyClass();
    23      }catch{}
    24  
    25      if(!dtorcalled){
    26          writefln("dtor not called");
    27          return 0;
    28      }else{
    29          writefln("dtor called");
    30          assert(0);
    31      }
    32  
    33      assert(0);
    34  }

Results in:
in ctor
dtor not called
in dtor

So, there seems to be two issues here:
1) the dtor is being called at the wrong place, much after the scope it's
declared within has gone away.

2) it's being called at all due to the constructor exiting via throw.

Upgrading to severity major since I can't think of any work around and proper
scope behavior is a fairly important language feature.


-- 



More information about the Digitalmars-d-bugs mailing list