Re: Bug in synchonous exception handling???
From: Doug Harrison [MVP] (dsh_at_mvps.org)
Date: 07/08/04
- Next message: Morten Aune Lyrstad: "Using Alt-key in MFC app"
- Previous message: Vengidi: "mouseclick"
- In reply to: Frank Delonge: "Bug in synchonous exception handling???"
- Next in thread: Frank Delonge: "Re: Bug in synchonous exception handling???"
- Reply: Frank Delonge: "Re: Bug in synchonous exception handling???"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 08 Jul 2004 11:55:44 -0500
Frank Delonge wrote:
>Hi,
>
>while migrating from VC6 to VC7 (and thus switching exception handling
>from the asynchronous model to synchronous), I discovered a crash in
>exception handling for the following situation:
>
>---------------------------------------------------------------------
>#include <afxtempl.h>
>#include <iostream>
>
>using namespace std;
>
>class Thrower {
>public:
> Thrower() : _i(2) {}
> virtual int* throwMe2() {throw _i; return &_i;}
> int _i;
>};
>
>int main()
>{
> CTypedPtrMap<CMapStringToPtr, CString, int*> myLookupListMap;
> Thrower* thrower = new Thrower();
>
> try {
> //--- works fine---
> // int* p(thrower->throwMe2());
> // myLookupListMap.SetAt("SomeName", p);
>
> //--- crashes, although there should not be any difference ---
> // if
> // - synchronous exception handling is used(/EHs) AND
> // - second argument to SetAt is a virtual function call where
> // vft is involved AND
> // - executable dynamically links against MFC AND
> // - At lease /O2 optimization is turned on
> myLookupListMap.SetAt("SomeName", thrower->throwMe2());
> }
> catch (int i) {
> cout << "catched " << i << endl;
> }
>
> delete thrower;
>}
>------------------------------------------------------------
>
>Looks like code optimization becomes somehow too optimistic about
>where exceptions may occur in the case of virtual functions.... thus,
>this would be a compiler bug!
>
>Is this true or am I doing something wrong here?
It's a bug. You should consider switching to /O1. It avoids the problem
here, and it's generally preferable to /O2 for several reasons. First, /O1
suffers from fewer bugs. Second, smaller code can be paradoxically faster if
it has significantly better caching/paging properties. Third, even if it's
slower, the old rule that says "90% of an application's time is spent in 10%
of the code" is mostly true; if anything, it's too conservative. It makes
little sense to use a buggier option to optimize code that doesn't matter,
and which can only benefit from being smaller.
-- Doug Harrison Microsoft MVP - Visual C++
- Next message: Morten Aune Lyrstad: "Using Alt-key in MFC app"
- Previous message: Vengidi: "mouseclick"
- In reply to: Frank Delonge: "Bug in synchonous exception handling???"
- Next in thread: Frank Delonge: "Re: Bug in synchonous exception handling???"
- Reply: Frank Delonge: "Re: Bug in synchonous exception handling???"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|