Microsoft KB Archive/80287

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

Article ID: 80287

Article Last Modified on 12/12/2003

-

APPLIES TO


 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 1.0 Standard Edition

-



This article was previously published under Q80287



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.



Visual Basic version 1.0 Documentation Errors
The information on page 226 of the &quot;Microsoft Visual Basic: Programmer's Guide&quot; version 1.0 manual in the section titled &quot;Unloading Forms&quot; 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 &quot;Visual Basic: Programmer's Guide&quot; 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
 Start a new project in Visual Basic. Form1 is created by default. Add a command button (Command1) to Form1. Change the caption of Command1 to Show Form Global Vars .  Add the following statements to the general Declarations section of Form1: Dim varX As Integer Dim arrayX(10) As String   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, &quot;#0&quot;) 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  Run the application by pressing the F5 key. Click the Show Form Global Vars button.</li> The values of StaticX, varX, and arrayX will print, even though the form has been unloaded.</li></ol>

Additional query words: 2.00 3.00

Keywords: kbprb KB80287

-

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

© Microsoft Corporation. All rights reserved.