Boost logo

Boost :

From: Jeffrey D. Paquette (paquette_at_[hidden])
Date: 2000-05-28 07:47:36


You've run afoul of Microsoft translating CPU exceptions into an exception
that cannot be caught except by catch(...) so you really cannot do much with
it, except gracefully exit.

X::~X has run twice because you've destroyed two X's by calling
crash_indirectly() twice. Change:
   catch(...) { }

to
     std::cerr << "Unknown exception caught"<< std::endl;

and your output becomes:

~X
Unknown exception caught
~X

To trap and convert processor exceptions to C++ objects (or something at
least catchable by type, look into set_se_translator() and be sure to
compile with /EHa.

At last, I've been able to contribute :)

--
Jeff Paquette
paquette_at_[hidden], paquette_at_[hidden]
http://www.atnetsend.net
-----Original Message-----
From: David Abrahams [mailto:abrahams_at_[hidden]]
Sent: Sunday, May 28, 2000 6:55 AM
To: boost_at_[hidden]
Subject: [boost] solution: Microsoft JIT debugging and catch(...)
The problem: This applies to programs generated by all compilers using
Microsoft-compatible EH when run outside a debugger. I plan to write a
longer
exposition, but I thought I should post this quickly.
----------
#include <iostream>
struct X { ~X() { std::cout << "~X" << std::endl; } };
void crash()
{
    *((char*)0) = 0;
}
void crash_indirectly()
{
    X x;
    crash();
}
int main()
{
   try {
       crash_indirectly(); // You never find out about this crash
   }
   catch(...) { }
   try {
       crash_indirectly(); // When post-mortem debugging starts,
   }                       //  X::~X has already run (twice).
   catch(...) {
       throw;
   }
}
-------------
The solution:
#include <windows.h>
void structured_exception_translator(
     unsigned int exception_code, EXCEPTION_POINTERS* pExp)
{
    UnhandledExceptionFilter(pExp);
    throw; // magic!
}
static void (*saved_translator)(unsigned, EXCEPTION_POINTERS*)
  = _set_se_translator(structured_exception_translator);
-------------
The line labelled "magic!" above is still a bit of a mystery to me. Without
it,
the debugger appears to re-start the program in the
structured_exception_translator, but the program immediately continues just
as
in the first example (except that the debugger has been invoked). If anyone
has
insight into this, I'd appreciate it. I'm sure it's a nitty-gritty Windows
thing, the sort of thing I try to avoid and had to muck about in too much
already to get this far ;). I plan to write a short article for the C++
Report
about this once I get that final detail worked out.
-Dave
------------------------------------------------------------------------
Missing old school friends?  Find them here:
http://click.egroups.com/1/4055/3/_/9351/_/959513157/
------------------------------------------------------------------------

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk