Microsoft KB Archive/269445

= FIX: Unloading Form from Control Array Event Causes Application Error =

Article ID: 269445

Article Last Modified on 10/5/2005

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Enterprise Edition Service Pack 4
 * Microsoft Visual Studio 6.0 Service Pack 4

-



This article was previously published under Q269445



SYMPTOMS
You have a Visual Basic project that has a form with control array. You unload the form inside of control array's event handler and when the form unloads, you set the form to be Nothing. The project works fine with Visual Studio 6.0 Service Pack 3 installed. However, if Visual Studio 6. 0 Service Pack 4 is installed on the computer, the application stops responding when the control array's event to unload the form is fired.



RESOLUTION
 To work around the problem for scenarios 1 and 2 (as described in the &quot;More Information&quot; section of this article), don't set the form to nothing when the form is unloaded.  To work around the problem for scenario 3, make a form-level reference to the form that will not lose scope while the form is active. To do so, add the following to the General Declarations section: Private f as Form 

A supported fix is now available from Microsoft, but it is only intended to correct the problem that is described in this article. Apply it only to computers that are experiencing this specific problem. This fix may receive additional testing. Therefore, if you are not severely affected by this problem, Microsoft recommends that you wait for the next service pack that contains this hotfix.

To resolve this problem immediately, contact Microsoft Product Support Services to obtain the fix. For a complete list of Microsoft Product Support Services phone numbers and information about support costs, visit the following Microsoft Web site:

http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

NOTE: In special cases, charges that are ordinarily incurred for support calls may be canceled if a Microsoft Support Professional determines that a specific update will resolve your problem. The typical support costs will apply to additional support questions and issues that do not qualify for the specific update in question.



STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.



MORE INFORMATION
There are at least three scenarios in which this bug appears.

Steps to Reproduce the Problem
Scenario #1  Create a Standard EXE Visual Basic Project under Visual Basic 6.0 with Visual Studio 6 Service Pack 4 installed. Form1 is created automatically. Press CTRL+T, and the add any non-intrinsic control into the components dialog box. For example, add &quot;Sheridan 3D Controls&quot; or &quot;Microsoft Windows Common Controls 6.0 (SP4)&quot;. Add an instance of SSCommand to Form1. Use the default name, which is SSCommand1.</li> Create a control array by giving the control an index of 0.</li>  Add the following code to Form1: Private Sub Form_Unload(Cancel As Integer) 'The following line causes a fault under SP4, when 'Unload Me' 'is called from an event in a control array. 'Comment this out to work around the bug. Set Form1 = Nothing End Sub

Private Sub SSCommand1_Click(Index As Integer) Unload Me End Sub </li> Save the project, and press F5 to run it.</li> Click the command button. Visual Basic stops responding and displays the following error message:

The instruction at &quot;0x004051ba&quot; referenced memory at &quot;0x0000007a&quot;. The Memory could not be &quot;written&quot;.

</li> Open the project in the Visual Basic integrated development environment again.</li>  Comment out the following line: Set Form1 = Nothing </li> Save the project, and then press F5 to run it. Click the command button. The form closes without a problem.</li></ol>

Scenario #2 <ol> Create a new UserControl.</li>  Add the following code: Public Event Click Private Sub UserControl_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) RaiseEvent Click End Sub </li> From the File menu, select Add Project, and then select Standard EXE.</li> Press CTRL+R to display the Project Explorer.</li> Right-click Project1, and select Set as Start Up.</li> Double-click Form1 to open the Form Designer, and then drop an instance of the newly created User Control on it. Use the default name of UserControl11.</li> Create a control array by giving the control an index of 0.</li>  Add the following code: Private Sub UserControl11_Click(Index As Integer) Unload Me End Sub

Private Sub Form_Unload(Cancel As Integer) Set Form1 = Nothing End Sub </li> <li>Save the project and press F5 to run it.</li> <li>Click the UserControl. Visual Basic fails and displays the following error message:

The instruction at &quot;0x004051ba&quot; referenced memory at &quot;0x0000007a&quot;. The Memory could not be &quot;written&quot;.

</li> <li>Open the project in Visual Basic again.</li> <li> Comment out the line: Set Form1 = Nothing </li> <li>Save the project, and then press F5 to run it.</li> <li>Click the UserControl. The form closes without a problem.</li></ol>

Scenario #3 <ol> <li>Create a new Standard EXE.</li> <li>Add a command button to Form1. Use the default name, which is Command1.</li> <li> Add the following code: Private Sub Command1_Click Dim f As New Form2 f.Show End Sub </li> <li>Add Form2 to the project.</li> <li>Press CTRL+T, and then add any non-intrinsic control into the components dialog box, such as &quot;Sheridan 3D Controls&quot; or &quot;Microsoft Windows Common Controls 6.0 SP4)&quot;.</li> <li>Add an instance of SSCommand to Form2. Use the default name, which is SSCommand1.</li> <li>Create a control array by giving the control an index of 0.</li> <li> Add the following code to Form2: Private Sub SSCommand1_Click(Index As Integer)    Unload Me End Sub                    </li> <li>Save the project, and then press F5 to run it.</li> <li>Click Command1. Form2 appears.</li> <li>Click SSCommand1. Visual Basic stops responding and displays the following with error message:

The instruction at &quot;0x004051ba&quot; referenced memory at &quot;0x0000007a&quot;. The Memory could not be &quot;written&quot;.

</li> <li>Open the project in Visual Basic again.</li> <li> Change the line Dim f as New Form2 in Form1 to: Set f = New Form2 Add the following code to the General Declarations section of Form1: Private f as Form2 </li> <li>Save the project, and then press F5 to run it.</li> <li>Click Command1. Form2 appears.</li> <li>Click SSCommand1. The form closes without a problem.</li></ol>

Additional query words: 0x004051ba 0x0000007a

Keywords: kbbug kbfix kbqfe KB269445

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.