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

ID: Q99846


The information in this article applies to:


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 query words: 1.00 1.50 2.00 2.50 crash


Keywords          : kb16bitonly 
Version           : 
Platform          : 
Issue type        : 

Last Reviewed: July 26, 1999