Microsoft KB Archive/247203: Difference between revisions

From BetaArchive Wiki
m (Text replacement - "<" to "<")
m (Text replacement - """ to """)
 
(2 intermediate revisions by the same user not shown)
Line 46: Line 46:
== SYMPTOMS ==
== SYMPTOMS ==


When trying to build an application that uses C++ exception handling, with global optimizations enabled, the catch handler within the try/catch block may not catch any of the exceptions that are thrown by the instructions in the try block. See the &quot;More Information&quot; section below for details.
When trying to build an application that uses C++ exception handling, with global optimizations enabled, the catch handler within the try/catch block may not catch any of the exceptions that are thrown by the instructions in the try block. See the "More Information" section below for details.


</div>
</div>
Line 63: Line 63:
<br />
<br />


# Disable global optimizations on a function-by-function basis using the optimize pragma with the &quot;g&quot; option. '''/Og-''' disables global optimization on a file-by-file basis.
# Disable global optimizations on a function-by-function basis using the optimize pragma with the "g" option. '''/Og-''' disables global optimization on a file-by-file basis.
# Use the asynchronous exception handling model by specifying the '''/EHa''' compiler option.
# Use the asynchronous exception handling model by specifying the '''/EHa''' compiler option.


Line 85: Line 85:
Compile Options : cl /GX /GR /Og test.cpp
Compile Options : cl /GX /GR /Og test.cpp
<pre class="codesample">//test.cpp
<pre class="codesample">//test.cpp
#include <iostream&gt;
#include <iostream>
using namespace std;
using namespace std;


Line 106: Line 106:
     try
     try
     {
     {
         pComp=dynamic_cast<B*&gt;(m_pComp);
         pComp=dynamic_cast<B*>(m_pComp);
     }
     }
     catch(exception&amp; exc)  //The exception is caught only if Global Optimizations are disabled.
     catch(exception& exc)  //The exception is caught only if Global Optimizations are disabled.
     {
     {
         cout << &quot;In Catch Block\n&quot;;
         cout << "In Catch Block\n";
         cout << &quot;Exception Caught\n&quot; <<endl;
         cout << "Exception Caught\n" <<endl;
     }
     }
}
}

Latest revision as of 13:50, 21 July 2020

Article ID: 247203

Article Last Modified on 1/22/2004



APPLIES TO

  • Microsoft Visual C++ 6.0 Enterprise Edition
  • Microsoft Visual C++ 6.0 Professional Edition
  • Microsoft Visual C++ 6.0 Standard Edition



This article was previously published under Q247203

SYMPTOMS

When trying to build an application that uses C++ exception handling, with global optimizations enabled, the catch handler within the try/catch block may not catch any of the exceptions that are thrown by the instructions in the try block. See the "More Information" section below for details.

CAUSE

The optimizer optimizes the try/catch block incorrectly.

RESOLUTION

Here are the two ways to work around this problem:

  1. Disable global optimizations on a function-by-function basis using the optimize pragma with the "g" option. /Og- disables global optimization on a file-by-file basis.
  2. Use the asynchronous exception handling model by specifying the /EHa compiler option.


STATUS

This bug was corrected in Microsoft Visual C++ .NET 2002.

MORE INFORMATION

Steps to Reproduce Behavior



Compile Options : cl /GX /GR /Og test.cpp

//test.cpp
#include <iostream>
using namespace std;

class CObject
{
    virtual function() {};  //necessary to make the class polymorphic
};


class B : public CObject
{
public:
    CObject* m_pComp;
    void Test() const;
};

void B::Test() const
{
    B* pComp;
    try
    {
        pComp=dynamic_cast<B*>(m_pComp);
    }
    catch(exception& exc)   //The exception is caught only if Global Optimizations are disabled.
    {
        cout << "In Catch Block\n";
        cout << "Exception Caught\n" <<endl;
    }
}

void main()
{
    B object;   //m_pComp is intentionally not initialized so that the exception occurs.
    object.Test();
}

Expected Output:
In Catch Block
Exception Caught

                



When the above code is compiled using /Og, then the control never enters the catch block and results in an access violation.

Keywords: kbbug kbpending KB247203