PRB: GP Fault From DestroyWindow() Call in VBX Event

Last reviewed: July 17, 1997
Article ID: Q99846
1.00 1.50 WINDOWS kbprg kbprb

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC) included with:

        - Microsoft Visual C++ for Windows, versions 1.0 and 1.5
    

SYMPTOMS

If an application calls the DestroyWindow() function in a VBX control event handler, a general protection (GP) fault occurs.

CAUSE

The application attempts to destroy the VBX control while processing a VBX event.

RESOLUTION

Edit the code to remove the DestroyWindow() call. Instead, post a WM_CLOSE message to the appropriate dialog box or control with the following code:

   PostMessage(WM_CLOSE);

MORE INFORMATION

This error typically occurs if a modeless dialog box contains a VBX control as a Cancel button and the event handler calls DestroyWindow() to terminate the dialog box.

The code example below is a sample dialog box message map and VBX event handler that demonstrate the problem. The message map processes a VBN_CLICK event from a 3D pushbutton VBX control. When the event occurs, the code calls the CMyDialog::OnClickCancel() handler. If the handler calls the DestroyWindow() function, a GP fault occurs. Replacing the DestroyWindow() call with a PostMessage() call eliminates this problem.

The PostMessage() call allows the application to finish processing the event before destroying the dialog box and VBX control.

Message Map

   BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
      //{{AFX_MSG_MAP(CMyDialog)
      ON_VBXEVENT(VBN_CLICK, IDC_COMMAND3D, OnClickCancel)
      //}}AFX_MSG_MAP
   END_MESSAGE_MAP()

Event Handler

void CMyDialog::OnClickCancel(UINT, int, CWnd* pControl, LPVOID)
{
   DestroyWindow();          // this call causes a protection violation
   // PostMessage(WM_CLOSE); // this call does not
}


Additional reference words: 1.00 1.50 2.00 2.50 crash
KBCategory: kbprg kbprb
KBSubcategory: MfcVBX
Keywords : kb16bitonly
Technology : kbMfc


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