form disposal

S

Steve

I have a method that creates of new form each time its called do i need
to dipose of the form when it closes or doe the garbage collector take care
of it?

code
private void button1_Click(object sender, EventArgs e)

{

frmImportData ImportData = new frmImportData();

ImportData.ShowDialog(); // when ImportData form closes do i need to
dispose?

}



rgds,Steve
 
T

Truong Hong Thi

I wouldn't call Dispose here. It should have been disposed by calling
Close in ImportData's code, or by click the X button.
 
M

Marc Gravell

Oddly enough, I always would... even though it perhaps should have been
done already; I find that the following default works well for
disposable items:

1: is it disposable?
2: do I feel ownership? (i.e. did I create / adopt it in this code
block)
3: is the scope contained? (i.e. it doesn't logically live beyound this
region, as non-modal forms would)

If I can answer "yes" to these, then I use "using", and this tends to
be the majority; so here I would:

using(SomeForm formInstance = new SomeForm()) {
// [*] note for later
formInstance.ShowDialog();
}

OK, in reality there isn't enough going on here that it is an issue,
but! Now imagine that there are a few lines of code at point [*]; now
it becomes less clear whether it is "clean" when abandoned through an
exception. It is /probably/ the case Dispose() mainly deals with
"hwnd"s, in which case they *probably* won't have even been issues
until .Show() / .ShowDialog(), but that is using inside knowledge of a
class, which defeats the purpose of the encapsualtion. Besides, I'm
sure I could create a Form class which calls CreateHandle() earlier
than normal, or obtains it's own disposable resources in the ctor.

The other advantage is this approach it works in all normal scenarios,
and saves you from having to ask "should I do x / y / z", which you can
only really answer by knowing too much about the internals of the class
in question. Which could change at any point. And yes, finalizers are
there to a point, but I always prefer deterministic disposal of
unmanaged resources.

Just a counterview...

Marc
 
D

Dave Sexton

Hi Everyone,

Furthermore, in the .NET 2.0 Framework, closing a Form by way of code, e.g.,
aForm.Close(), or by the clicking the close button, does not make a call to
Dispose if the Form is shown using the ShowDialog method.

All Forms opened with ShowDialog should be explicitly disposed, not only
because they are IDisposable, as Marc pointed out, but also because they won't
be disposed until finalization otherwise (AFAIK).

There is one major reason for this behavior. Modal dialogs are meant in many
cases to return a value to the "opener", such as the DialogResult and choices
that the user has made. Disposing the Form automatically could be problematic
in these cases.
 
L

Laura T

In this case Dispose() is necessary, as documented
(http://msdn2.microsoft.com/en-us/library/aw58wzka.aspx):

"
Dispose will be called automatically if the form is shown using the Show
method. If another method such as ShowDialog is used, or the form is never
shown at all, you must call Dispose yourself within your application.
"

If the code is not performance critical, make Dispose() a habit. Use it
whenever it is possibile, even when it could be unnecessary. It does not
hurt, but it can (will) help.
 
P

PS

Steve said:
I have a method that creates of new form each time its called do i need
to dipose of the form when it closes or doe the garbage collector take
care of it?

The using statement is what I use.

using (frmImportData ImportData = new frmImportData())
{
ImportData.ShowDialog();
}

PS
 
T

Truong Hong Thi

Furthermore, in the .NET 2.0 Framework, closing a Form by way of code, e.g.,
aForm.Close(), or by the clicking the close button, does not make a call to
Dispose if the Form is shown using the ShowDialog method.

Just look at MSDN for Form.Close, it does dispose the dialog. There is
only one exception:
"The one condition when a form is not disposed on Close is when it is
part of a multiple-document interface (MDI) application, and the form
is not visible."

For disposable objects, it is largely understood that Close is just
another name for Dispose in the cases when the verb "Close" is more
appropriate. It would be a trap if Close means something else.

However, when X button of a modal dialog is clicked, the form is
hidden, not closed, and DialogResult is set to Cancel.

Regards,
Thi
 
D

Dave Sexton

Hi Truong,

You are incorrect. When a Form shown by ShowDialog is closed by clicking the
close button with your mouse, the Close method is called, but not Dispose.
The Closing and Closed events are raised as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top