Closing a form

  • Thread starter Thread starter thechaosengine
  • Start date Start date
T

thechaosengine

Hi All,

When you close a form using myForm.Close(), does that actually destroy the
form and make it available for garbage collection?

I just want to make sure that I'm not supposed to do something to it after
to make sure that the memory can be claimed back.

I also have a timer object declared globally at the top of my main form.
I only need to use the object to control the Splash screen for the application.
After calling Stop() on it what would be the best way to get rid of it?

Thanks to anyone who can advise

Kind Regards

tce
 
Two things come to mind:

(1) The Close() method is not called for you when you close a Form that has
been shown using the ShowDialog() method. See here
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html
/frlrfsystemwindowsformsformclassshowdialogtopic1.asp) for exactly what I
mean by this.

(2) Always remember to call the Dispose() method on an object, if the method
exists, when you are done with it. This is also important in relation to
point 1. You may consider implementing the "Dispose Pattern" in your code.
See the following links for more on this.
http://msdn.microsoft.com/library/d.../en-us/cpgenref/html/cpconFinalizeDispose.asp
http://msdn.microsoft.com/library/d...guide/html/cpconimplementingdisposemethod.asp
 
thechaosengine said:
When you close a form using myForm.Close(), does that actually destroy the
form and make it available for garbage collection?

Yes, if the form is not referenced elsewhere, or existing references to the
form are not accessible for the program any more (for example, circular
references). Notice that forms shown using 'ShowDialog' are not disposed
automatically when calling their 'Close' method, and thus it's suggested to
call 'Dispose' after calling close (not calling 'Dispose' will /not/ prevent
the form from being garbage collected):

\\\
Dim f As New FooForm()
f.ShowDialog()
f.Dispose()
///
 
Hi Tim,

Thanks for your help. The articles you mentioned talk about operating on
unmanaged code. I actually thought that Forms would be managed. Is this not
the case?

I've never seen dispose being called manually in simple forms applications
so I'm not sure what I'm supposed to do.

At the moment I am just assigning the form variable to null so that there
are no longer any references to it.

It doesnt seem like the right thing to do though.

Any ideas?

Thanks again
 
The articles you mentioned talk about operating on
unmanaged code. I actually thought that Forms would
be managed. Is this not the case?
Yes, Forms are managed and the articles are dealing with managed code.
Sometimes managed objects need to have handles, tokens, to unmanaged objects
in order to do their job. The "Dispose Pattern" exists as just one way, a
recommended way, to ensure that any unmanaged resources are released when
the object is destroyed. Calling the Dispose() method explicitly is a good
idea, even though through the "Dispose Pattern" the unmanaged resources are
usually released in the Finalizer. However, the Finalizer is seen as a last
resort to ensure that these resources are released in case the end-developer
forgets to call Dispose().
 
Back
Top