Microsoft KB Archive/190519

= BUG: UserControl Causes VB IDE to Crash on Shutdown =

Article ID: 190519

Article Last Modified on 5/13/2003

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q190519



SYMPTOMS
A UserControl is added to a Visual Basic project. When you try to exit the IDE, Visual Basic causes an Invalid Page Fault (IPF) and terminates. This is preceded by the error message:

The memory could not be "written".



CAUSE
The problem is that the UserControl has two module level variables, one that holds onto a reference to the UserControl itself (Set m_oMe = Me) and another that holds a reference to control's parent (Set m_oParent = Extender.Parent). This causes a circular reference count on the control and the container. When you try to exit the IDE, Visual Basic destroys the design-time form container and attempts to release the UserControl instance. Because the control has a reference to itself and the form, Visual Basic cannot properly destroy the objects; consequently an Invalid Page Fault occurs.



RESOLUTION
Avoid setting module level variables that reference both a control and its container. Setting the variables to Nothing in the UserControl_Terminate event will not avoid the error because the Terminate event will never fire if the control has a circular reference.



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



Steps to Reproduce Behavior
 Start a new ActiveX Control project in Visual Basic. UserControl1 is created by default.  Copy the following code to the Code window of UserControl1: Option Explicit Private m_oMe As Object Private m_oParent As Object

Private Sub UserControl_InitProperties Set m_oMe = Me        Set m_oParent = UserControl.Extender.Container Let UserControl.BackColor = &HFF0000 End Sub

Private Sub UserControl_Terminate Set m_oMe = Nothing Set m_oParent = Nothing End Sub

 [Optional] Save and compile Project1. Close UserControl1's design window. Choose Add Project from the File menu and add a Standard EXE project to the project group. Form1 is created by default.</li> Add the UserControl you created to Form1.</li> Now exit the Visual Basic IDE, by pressing ALT+Q or the close button on the title bar. When prompted to save changes, select "No". The IDE will crash on exit.</li></ol>

<div class="references_section">