Clearing memory when closing a windows form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a windows form that I run from a menu. It uses a number of public
variables that are accessed throughout the code. When I exit back to the
menu and then go back into the form, the values of the variables are still as
they were when I left.

Is there a way to clear all of these variables when exiting the form?
 
Well, seems you gave the answer yourself, i.e. reset those variables when
you exit the form, right? Ok, handle the event 'exit form' and give the
public variables your initial value.
 
I was hoping for something a little more systematic, since I have a couple of
dozen public variables. In every other language I have ever used, there is
always a way to clear memory when ending a program. Most do it
automatically. Is there a method that goes along with close form that does
this?
 
no such method that i know of, but you could for example collect your vars
in a key/value collection so you can loop over the vars. if they all are of
a type that has a reset method you could just call that for every var

any good?
 
clearing memory? when the process goes out of scope so does the memory it
owns (with some exceptions but that is another story) nowadays we use the
garbage collector in normal cases but now we are deviating from your
question

if you have a public reset method that resets the variables, you can call
that when you exit the other form, right?
 
Ok, that bit about processing going out of scope sounds interesting. Can I
force my process to go out of scope when I close the form? Or can I get the
menu that opens the form to get the form to go out of scope?? Shouldn't
closing the form cause it to go out of scope automatcially?

If not, I guess I'm going to have to clear all variables manually.
 
no, no, the process contains all else and that includes your forms, i am
talking win32 process here

the form is closed and vanishes but your variables are in another scope so
their values are not affected

you said you accessed public variables, i assume they are in another class
then the form you close

when you close the form an event is raised that you can handle, from that
handler you can reset your variables

are my assumptions correct?
 
no, why? the vars are not in the form that accesses them, just hook up a
handler to that form that resets them is all that's needed
 
That's what I dont' understand. The public variables are all declared in the
form that is being called by the menu. So when I do me.close() in the form
shouldn't that clear out all variables for that form? I tried declaring the
variables as protected instead of public but that did not work either.

Yes, I can manually intialize my variables, but I've never had to do this
in any other language. Even in the old DBASE language, there was a clear
memory statement.

I guess .Net just doesn't work that way.
 
that clarifies something, i thought you had the vars outside of the form
because you mentioned public
my assumption there was wrong

when the vars are declared within the form they keep their values as long as
the form exists
if closing the form does not destroy it then the vars keep their values

so how do you close your form?

it seems it is not destroyed since the vars it contains keep their values
that also suggests later on you 'open' the form again and see the vars with
their previous values, that is they are not in some initial state

do you have a form member elsewhere that you want to instantiate one time
only and subsequently show it on menu click and hide it from the form
itself?
 
It really sounds like you should use a new instance of the form, unless
between closing and re-opening the form there is some state that you wish to
keep on that form?

Closing a form causes it to get disposed, unless it was displayed using
ShowDialog() (if I remember correctly). Calling Show() on it again will give
you an ObjectDisposedException.
 
Back
Top