 |
yronium Level: Moderator

 Registered: 14-04-2002 Posts: 907
|
Re: Unwanted retention of variable
Well, Dave, the ideas of mine are: how do you unload the form? Are you sure you really unloaded it (I mean, not just hide it)? And, pardon, what module is the variable declared in? The child form module? (Of course it has to, but...). Are you sure you didn't declare another global variable with the same name (that can bring to unexpected results)? In poor words, I only can think to some hidden mistake, for yes: if you unload a form, all its module's variables are destroyed and their allocated memory space is signed as free.
Try checking the var state step by step with the F8 key, and checking the Local Variables window.
Sorry I can't tell you more, maybe somebody else...
____________________________
Real Programmer can count up to 1024 on his fingers
|
|
13-02-2006 at 12:05 AM |
|
|
JLRodgers Level: Moderator
 Registered: 04-04-2002 Posts: 1617
|
Re: Unwanted retention of variable
Everything that was said plus:
are you sure you don't set the variable in a routine at the form's load (i.e. form_[activate|load|etc])
more likely than not, it's being set to true due to something in the form itself, or by other code. (assuming the other points given aren't true)
____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)
|
|
13-02-2006 at 07:49 AM |
|
|
JLRodgers Level: Moderator
 Registered: 04-04-2002 Posts: 1617
|
Re: Unwanted retention of variable
Freaky.... if you want something like that to work it won't.
But eh, if you do a "load form1" then put in the form's unload (or probably the calling form, but not really feasible in a MDI interface) "set form1 = nothing" it clears it.
Probably it's keeping a reference until 1) the program closes, or 2) you outright tell it "I'm done, get rid of the form"
____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)
|
|
13-02-2006 at 04:33 PM |
|
|
Goran Level: Moderator
 Registered: 16-05-2002 Posts: 1681
|
Re: Unwanted retention of variable
quote: I've done some research. According to the Microsoft Press publication: Hardcore Visual Basic by Bruce McKinney, private variables and their values are retained after a form is unloaded. Only if the form is explicitly set to NOTHING or the app terminates are they cleared down and the space freed up.
This is not only the issue with VB5, but is present in vb6 also. The problem is, when the form is loaded for the first time, Initialize event is fired, and after you unload form, the Terminate event wont fire, so basically form is still in the memory (and its module variables). If you load/show the form again, no initialize event will be fired, since the form is actually aready initialized. Terminate event will occur, either when you close tha app, or set the form explicitly to nothing.
So, to go around it, you can:
1) set the form to nothing (as JL advised you)
2) in from unload event, reset all module variables, as you suggested
3) there are other ways (like reseting the variables in form_Load event, but then you would need to have Show method as the first line in the public procedure you call, so form_load is executed first), but these two are the easiest to maintain (at least to me).
____________________________
If you find the answer helpful, please mark this topic as solved.
|
|
21-02-2006 at 10:46 PM |
|
|
|
|
 |
 |