From mboxrd@z Thu Jan 1 00:00:00 1970 From: mf@cfdrc.com To: gcc-gnats@gcc.gnu.org Subject: c++/4250: destructor is not called during stack unwinding after exception Date: Thu, 06 Sep 2001 09:46:00 -0000 Message-id: <20010906163902.8125.qmail@sourceware.cygnus.com> X-SW-Source: 2001-09/msg00125.html List-Id: >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 #include 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; }