PRB: Form Global (Static) Data Is Preserved After Form Unload (80287)
The information in this article applies to:
- Microsoft Visual Basic Standard Edition for Windows 2.0
- Microsoft Visual Basic Standard Edition for Windows 3.0
- Microsoft Visual Basic Professional Edition for Windows 2.0
- Microsoft Visual Basic Professional Edition for Windows 3.0
- Microsoft Visual Basic Standard Edition for Windows 1.0
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.
Modification Type: | Major | Last Reviewed: | 12/12/2003 |
---|
Keywords: | kbprb KB80287 |
---|
|