c++/4250: destructor is not called during stack unwinding after exception

mf@cfdrc.com mf@cfdrc.com
Thu Sep 6 09:46:00 GMT 2001


>Number:         4250
>Category:       c++
>Synopsis:       destructor is not called during stack unwinding after exception
>Confidential:   no
>Severity:       critical
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Thu Sep 06 09:46:00 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator:     Michal Furmanczyk
>Release:        gcc 3.0 (build with --enable-threads)
>Organization:
>Environment:
i686-pc-linux-gnu
>Description:
When there is a try-catch block in function, local variables located before this block and the exception is thrown in this block but not catch the destructors of local variables are not called. Exception is catch in another try-catch block outside the function, therefore after the point when local variables should be destroy.
>How-To-Repeat:
Compile this code with: g++ -o test test.cpp
The result program print only: ctor called
it should print: ctor called, dtor called
The same is with -O1 and -O2 option.

>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: text/plain; name="test.cpp"
Content-Disposition: inline; filename="test.cpp"

#include <iostream>
#include <stdexcept>

class Test {
public:
  Test() { std::cerr << "ctor called" << std::endl; }
  ~Test() { std::cerr << "dtor called" << std::endl; }
};

void e() {
  throw std::logic_error("test");
}

void f() {
  Test t;
  try {
    e();
  }  
  catch(std::runtime_error&) {
  }
}

int main() {
  try {
    f();
  }
  catch(std::exception&) {
  }
  return 0;
}



More information about the Gcc-bugs mailing list