Re: Bug in synchonous exception handling???

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Doug Harrison [MVP] (dsh_at_mvps.org)
Date: 07/08/04


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++


Relevant Pages

  • Re: What is Greatest Common Divisor of (0,0)
    ... other people are not all stupid and that you are not the only truly ... And enable him to fix the bug. ... > exception handling, and if he do that, ... > then correct data flow will never do that. ...
    (borland.public.delphi.language.basm)
  • Re: Fastcode CPUID Unit
    ... Dennis wrote: ... Regards ... The only "bug" is using exception handling in the cpuid unit. ...
    (borland.public.delphi.language.basm)
  • Re: Fastcode CPUID Unit
    ... > The only "bug" is using exception handling in the cpuid unit. ... Do you want to write a few words about FastMM4 versus RTL MM for our ... Regards ...
    (borland.public.delphi.language.basm)