FIX: Unloading Form from Control Array Event Causes Application Error

Article translations Article translations
Article ID: 269445 - View products that this article applies to.
This article was previously published under Q269445
This article has been archived. It is offered "as is" and will no longer be updated.
Expand all | Collapse all

On This Page

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 "More Information" 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
  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.
  2. Press CTRL+T, and the add any non-intrinsic control into the components dialog box. For example, add "Sheridan 3D Controls" or "Microsoft Windows Common Controls 6.0 (SP4)".
  3. Add an instance of SSCommand to Form1. Use the default name, which is SSCommand1.
  4. Create a control array by giving the control an index of 0.
  5. 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
    					
  6. Save the project, and press F5 to run it.
  7. Click the command button. Visual Basic stops responding and displays the following error message:
    The instruction at "0x004051ba" referenced memory at "0x0000007a". The Memory could not be "written".
  8. Open the project in the Visual Basic integrated development environment again.
  9. Comment out the following line:
    Set Form1 = Nothing
    					
  10. Save the project, and then press F5 to run it. Click the command button. The form closes without a problem.

Scenario #2
  1. Create a new UserControl.
  2. 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
    					
  3. From the File menu, select Add Project, and then select Standard EXE.
  4. Press CTRL+R to display the Project Explorer.
  5. Right-click Project1, and select Set as Start Up.
  6. 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.
  7. Create a control array by giving the control an index of 0.
  8. 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
    					
  9. Save the project and press F5 to run it.
  10. Click the UserControl. Visual Basic fails and displays the following error message:
    The instruction at "0x004051ba" referenced memory at "0x0000007a". The Memory could not be "written".
  11. Open the project in Visual Basic again.
  12. Comment out the line:
    Set Form1 = Nothing
    					
  13. Save the project, and then press F5 to run it.
  14. Click the UserControl. The form closes without a problem.

Scenario #3
  1. Create a new Standard EXE.
  2. Add a command button to Form1. Use the default name, which is Command1.
  3. Add the following code:
    Private Sub Command1_Click()
        Dim f As New Form2
        f.Show
    End Sub
          
    					
  4. Add Form2 to the project.
  5. Press CTRL+T, and then add any non-intrinsic control into the components dialog box, such as "Sheridan 3D Controls" or "Microsoft Windows Common Controls 6.0 SP4)".
  6. Add an instance of SSCommand to Form2. Use the default name, which is SSCommand1.
  7. Create a control array by giving the control an index of 0.
  8. Add the following code to Form2:
    Private Sub SSCommand1_Click(Index As Integer)
        Unload Me
    End Sub
          
    					
  9. Save the project, and then press F5 to run it.
  10. Click Command1. Form2 appears.
  11. Click SSCommand1. Visual Basic stops responding and displays the following with error message:
    The instruction at "0x004051ba" referenced memory at "0x0000007a". The Memory could not be "written".
  12. Open the project in Visual Basic again.
  13. 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
          
    					
  14. Save the project, and then press F5 to run it.
  15. Click Command1. Form2 appears.
  16. Click SSCommand1. The form closes without a problem.

Properties

Article ID: 269445 - Last Review: February 28, 2014 - Revision: 3.2
APPLIES TO
  • Microsoft Visual Basic 6.0 Enterprise Edition Service Pack 4
  • Microsoft Visual Studio 6.0 Service Pack 4
Keywords: 
kbnosurvey kbarchive kbbug kbfix kbqfe KB269445

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com