
Making sure a form unloads before a program exits
Hi,
Quote:
> I thought, correct me if I'm wrong, that Set Form1 = Nothing and Unload
> Form1 were the same. I don't think that Unload Me just close the form...
it
> unload it...
Unload does unload the GUI portion of the Form, but depending on what is in
the class module, it may not free all of that. In other words, if you have
public or static variables etc. in the Form, simply calling Unload won't
free up the resources required by the module.
Bob Butler posted a good explanation of this over in
microsoft.public.vb.general.discussion.
See the message called: a question about static variable
To quote part of his message:
Forms are divided into two pieces. The first part is essentially a class
module and has the code, module-level variables and local static variables.
The second part is the GUI with the controls and other visual elements.
When you click the X you are unloading the GUI part but not necessarily the
class part. All control properties will be reset to defaults if the form is
reloaded but unless the reference to the class part is released the
variables stay.
David
lilchips.com
Quote:
> Hi David!
> > Personally I use the "Set <form> = Nothing" statement in appropriate
> places
> > for any Forms that I load.
> I do Unload all the forms right after their use. I use the code showed
> in the other post to make sure I haven't forgot any forms or reload one by
> mistake.
> > Otherwise not all resources may be freed by just closing the Form,
causing
> > your application to have a larger footprint than is necessary.
> I thought, correct me if I'm wrong, that Set Form1 = Nothing and
Unload
> Form1 were the same. I don't think that Unload Me just close the form...
it
> unload it...
> > Although it is fine for smaller applications, for large applications
with
> > many dialogs, waiting until the "main" Form's Unload Event is fired
> doesn't
> > make the best use of resources.
> I agreed totaly! ;O)
> --
> Best Regards
> Zoury
> _________________________________
> Thanks to post replies to the newsgroup :O)
> > Hi,
> > Personally I use the "Set <form> = Nothing" statement in appropriate
> places
> > for any Forms that I load.
> > Otherwise not all resources may be freed by just closing the Form,
causing
> > your application to have a larger footprint than is necessary.
> > Although it is fine for smaller applications, for large applications
with
> > many dialogs, waiting until the "main" Form's Unload Event is fired
> doesn't
> > make the best use of resources.
> > David
> > lilchips.com
> > > If you destroy all the objects and unload all your forms you won't
have
> to
> > > use the "Evil End".
> > > Try this :
> > > Private Sub Form_Unload(Cancel As Integer)
> > > Dim frm As Form
> > > For Each frm In Forms
> > > Unload frm
> > > Next frm
> > > Set frm = Nothing
> > > End Sub
> > > if everything has been destroyed properly your program should stop
> > correctly
> > > ;O)
> > > --
> > > Best Regards
> > > Zoury
> > > _________________________________
> > > Thanks to post replies to the newsgroup :O)
> > > > Is there a standard way to make sure that a form unloads
> > > > completely before a program ends?
> > > > I have a global function EndProgram which looks like this:
> > > > Public Sub EndProgram()
> > > > ' Do cleanup routines
> > > > ' set global objects to be nothing (not forms)
> > > > If Not Form1 Is Nothing Then
> > > > Unload Form1
> > > > End If
> > > > If Not Form2 Is Nothing Then
> > > > Unload Form2
> > > > End If
> > > > Set Form1 = Nothing
> > > > Set Form2 = Nothing
> > > > End
> > > > End Sub
> > > > Here are my concerns / questions:
> > > > 1. I'm feeling a bit bad about calling the
> > > > function "End", but I'm finding it very difficult to make
> > > > sure that every part of every form is properly destroyed.
> > > > Are there strategies to make that process more transparent?
> > > > 2. Is there a better way to force a form to unload?
> > > > Thanks,
> > > > Brian