PRB: Form Global (Static) Data Is Preserved After Form Unload

ID: Q80287


The information in this article applies to:
  • Microsoft Visual Basic Standard and Professional Editions for Windows, versions 2.0, 3.0
  • Microsoft Visual Basic programming system for Windows, version 1.0


SYMPTOMS

Static data allocated from a form does not get deallocated when you unload the form. Static data stored in a form consists of:

  • Arrays or variables dimensioned in the general Declarations section of a form.


  • Arrays or variables declared in a Sub or Function procedure using the Static keyword.


  • All local variables and arrays allocated in a Sub or Function procedure where the procedure name is preceded by the keyword Static.


All static data is allocated in a global area of memory managed by Visual Basic. Unloading the form does not cause this memory to be deallocated.


RESOLUTION

Although the data is maintained after the form is unloaded, you cannot access this data from any other form or module. You must reload the form to access the data.

To deallocate arrays, use the ReDim statement to allocate the array dynamically as needed. To unload variables, use local variables instead of static variables. If you use local variables instead of static variables, the local variables are deallocated upon exit from the procedure in which they were allocated.

In version 1.0 or 2.0, Visual Basic preserves the data until the program terminates. There is no way to cause static data in the general Declarations section to be deallocated when the form is unloaded. For example, the Erase statement will not cause memory to be deallocated for arrays dimensioned in the general Declarations section.

In version 3.0, you can deallocate the memory by setting the form to nothing. This forces Visual Basic to unload the module data segment (the place where the memory is allocated) as in this example:


   Unload Form2
   Set Form2 = Nothing 


STATUS

This behavior is by design.


MORE INFORMATION

Visual Basic version 1.0 Documentation Errors

The information on page 226 of the "Microsoft Visual Basic: Programmer's Guide" version 1.0 manual in the section titled "Unloading Forms" implies that all data in a form is lost after the form is unloaded using the Unload statement. However, this does not apply to these types of data:

  • Any variable or array that is dimensioned in the general Declarations section of the form.


  • Any static variable or array that has been declared within a Sub or Function procedure.


  • Any local variable or array that has been allocated in a static Sub or Function procedure.


The following statement on page 226 of the "Visual Basic: Programmer's Guide" is incorrect:
Any data stored in the form is lost unless you have saved it to a file.
This statement should be changed to read as follows:
Any data stored in the form, with the exception of static variables and arrays, is lost unless you have saved it to a file. The values of static arrays and variables are preserved after the form is unloaded.

Steps to Reproduce Behavior

  1. Start a new project in Visual Basic. Form1 is created by default.


  2. Add a command button (Command1) to Form1.


  3. Change the caption of Command1 to Show Form Global Vars.


  4. Add the following statements to the general Declarations section of Form1:
    
       Dim varX As Integer
       Dim arrayX(10) As String 


  5. Add the following code to the Command1_Click event procedure of Command1:
    
       Sub Command1_Click ()
    
          Static StaticX
    
          StaticX = 1           ' Initialize the form global variables.
          varX = 10
          For i = 0 To 10
             arrayX(i) = Format$(i, "#0")
          Next i
    
          Unload Form1
          Form1.Show            ' Reload and show form.
                                ' Values of varX and arrayX will still be
                                ' preserved.
          Print StaticX         ' Print the values to the form.
          Print varX
          For i = 0 To 10
             Print arrayX(i)
          Next i
    
       End Sub 


  6. Run the application by pressing the F5 key. Click the Show Form Global Vars button.


  7. The values of StaticX, varX, and arrayX will print, even though the form has been unloaded.


Additional query words: 2.00 3.00

Keywords :
Version :
Platform :
Issue type :


Last Reviewed: September 16, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.