BUG: App with CFormView Causes Stack Overflow or GP Fault

Last reviewed: July 22, 1997
Article ID: Q134884
The information in this article applies to:
  • The Microsoft Foundation Classes (MFC) included with: - Microsoft Visual C++ for Windows, versions 1.5, 1.51, 1.52

SYMPTOMS

An application that uses a CFormView-derived class as its view may go into an infinite loop as a result of repeatedly creating CFormView windows. Depending on the compiler and linker settings, this problem may surface as a memory exception or a stack overflow.

CAUSE

When a CFormView receives the focus, it re-sets the focus to the control in the CFormView that previously had the focus. The HWND of the control that had focus last is saved in a CFormView protected member variable, m_hWndFocus. CFormView::OnSetFocus() calls ::SetFocus(m_hWndFocus)if m_hWndFocus is a valid window.

The problem occurs because one of the constructors for CFormView does not initialize this member variable. As mentioned, the CFormView::OnSetFocus() function sets focus to m_hWndFocus if it is a valid window. The problem occurs when this variable's uninitialized value happens to be the same as the HWND of the formview. ::SetFocus(m_hWndFocus) gives the focus to the CFormView, which causes OnSetFocus() to be called for the CFormView again. This causes the application to go into an infinite loop.

RESOLUTION

In the constructor of the CFormView-derived class that takes the ID of the template as an argument, initialize the m_hWndFocus variable to NULL. The constructor that takes an LPCSTR for an argument already does this. For example:

   CMyFormView::CMyFormView(UINT nIDTemplate)
      : CFormView(nIDTemplate)
   {
     // ADD THE FOLLOWING LINE TO THIS CONSTRUCTOR
     m_hWndFocus = NULL;
   }

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.


Additional query words: 2.50 2.51 2.52
Keywords : kb16bitonly kbprg MfcMisc kbbuglist
Technology : kbMfc
Version : 1.5 1.51 1.52
Issue type : kbbug


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: July 22, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.