There is my code:
#include <string> #include <tr1/functional> using namespace std; using namespace std::tr1; using namespace std::tr1::placeholders; class Event { public: typedef std::tr1::function<void()> Handler; void set(Handler h) { m_handler = h; } template<typename T, typename F> void set(T * obj, F memfn) { set(std::tr1::bind(memfn, obj)); } void operator()() { m_handler(); } static void fire(Event * event) throw () { (*event)(); } Handler m_handler; }; class BuggyHandler { public: BuggyHandler() { } BuggyHandler(Event * b) : bar(b) { bar->set(this, &BuggyHandler::HandleEvent); } void HandleEvent() { // throw std::length_error std::string().append(std::numeric_limits<size_t>::max(), '0'); } private: Event * bar; }; void get_correct_stacktrace() { Event bar; BuggyHandler handler(&bar); bar(); } void get_incorrect_stacktrace() { Event bar; BuggyHandler handler(&bar); Event::fire(&bar); } int main(int argc, char **argv) { int opt = atoi(argv[1]); if (opt) get_correct_stacktrace(); else get_incorrect_stacktrace(); }
When i call / test 1, I can get the correct stack trace from the kernel:
#0 0xffffe410 in __kernel_vsyscall ()
The throw exception is test.cc:54
When i call / test 0, I can get the wrong stack trace from the kernel:
#0 0xffffe410 in __kernel_vsyscall ()
There is no throw exception exception.
My compiler is "gcc (GCC) 4.1.2 20070115 (preview) (SUSE Linux)"
If compiled with "-fno-exceptions", both methods generate the correct stack trace.
What is the reason?
source share