Microsoft KB Archive/134884

From BetaArchive Wiki

BUG: App with CFormView Causes Stack Overflow or GP Fault

Q134884



The information in this article applies to:


  • The Microsoft Foundation Classes (MFC), included with:
    • Microsoft Visual C++, 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 : kbprogramming kb16bitonly kbnokeyword kbMFC kbVC
Issue type : kbbug
Technology : kbAudDeveloper kbMFC


Last Reviewed: May 8, 2001
© 2001 Microsoft Corporation. All rights reserved. Terms of Use.