Re: Handle C++ exception and structured exception together
* Pavel:
Alf P. Steinbach wrote:
I think it's as concrete as can be described with words, no hinting.
Double destruction example would just be example of destroying twice,
which is easy to do when using manual destruction instead of RAII,
especially in code where that destruction has to be redundantly
repeated (as is the case with MFC exception handler code).
Memory leak + disaster example code:
void throwX( char const s[] ) { throw std::runtime_error( s ); }
struct Base()
{
virtual ~Base() {}
virtual void foo() = 0;
void bar()
{
try { foo(); } catch( ... ) { throwX( "Base::bar()" ); }
}
};
struct SillyDerived: Base
{
void foo() { int* p = 0; *p = 666; }
};
Now with general SEH -> C++ exception translation enabled, and that
SEH nullpointer exception translated to a C++ throw of pointer to
dynamically allocated object, a call to bar() leaks memory, and much
worse, in the general case where a nullpointer exception indicates
something gone horribly awry, is likely to leave the program in an
unstable state (memory corruption, invalid assumptions) => disaster.
I agree this example won't work.. but why would anybody use catch(...)
for structured exception if the point is to catch CseException or
CException?
Especially with virtual functions, but also with function pointers and
templated functionality, at the point where you write the catch(...) you
don't know what code is executed, what exceptions can occur, and you
don't write catch(...) in order to use a language extensions that makes
this catch SEH exceptions, but in order to catch any /C++/ exception.
And in particular, that catch(...) can reside in existing years old code
that calls your new code.
E.g. Base above might be a SuperDuper library class from Microsoft
(quickly searching through the MS code on my machine, however, I found
only 1 instance of this pattern), and SillyDerived your new shiny class,
throwing SEH exception by accident.
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?