FIX: DYNAMIC_DOWNCAST & STATIC_DOWNCAST Causes Stack Overflow

Last reviewed: September 19, 1997
Article ID: Q151070
4.10 WINDOWS NT kbprg kbbuglist kbfixlist

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC), included with:

          Microsoft Visual C++, 32-bit Edition, version 4.1
    

SYMPTOMS

When the DYNAMIC_DOWNCAST and STATIC_DOWNCAST macros are called, and a pointer to a "const" object is passed, you see an error message similar to the following in debug mode:

  Unhandled exception in downcast.exe (MFC40D.DLL); Stack Overflow.

In release builds, the application hangs.

CAUSE

The DYNAMIC_DOWNCAST and STATIC_DOWNCAST macros invoke the AfxDynamicDownCast and AfxStaticDownCast functions respectively. A code defect in Visual C++ version 4.1 causes these calls to be recursive if the object pointer passed as the second parameter to either macro points to a "const" object. The end result is a stack overflow.

RESOLUTION

To work around this problem, remove the "const" attribute for the object pointer in the call to the appropriate DOWNCAST macro. You can do this by using the "const_cast" operator as follows:

   //CMyDlg is derived from CDialog
   const CMyDlg dlg;

   //pdlg is a pointer to a const object
   const CMyDlg* pdlg = &dlg;

   DYNAMIC_DOWNCAST(CDialog, const_cast<CMyDlg*>(pdlg));

   // or

   STATIC_DOWNCAST(CDialog, const_cast<CMyDlg*>(pdlg));

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This bug was corrected in Visual C++ 32-bit Edition version 4.2.


Additional reference words: 4.00 4.10 4.20 vcbuglist400 vcfixlist420
KBCategory: kbprg kbbuglist kbfixlist
KBSubcategory: MfcMisc


Keywords : MfcMisc kbbuglist kbfixlist kbprg
Technology : kbMfc
Version : 4.10
Platform : NT WINDOWS
Solution Type : kbfix


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 19, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.