By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,953 Members | 1,438 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,953 IT Pros & Developers. It's quick & easy.

form disposal

P: n/a

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
Oct 20 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a

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

Steve wrote:
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
Oct 20 '06 #2

P: n/a
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

Oct 20 '06 #3

P: n/a
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.

--
Dave Sexton

"Marc Gravell" <ma**********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
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

Oct 20 '06 #4

P: n/a
In this case Dispose() is necessary, as documented
(http://msdn2.microsoft.com/en-us/lib...w58wzka.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.
"Truong Hong Thi" <th*****@gmail.comha scritto nel messaggio
news:11**********************@m7g2000cwm.googlegro ups.com...
>
I wouldn't call Dispose here. It should have been disposed by calling
Close in ImportData's code, or by click the X button.

Steve wrote:
>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

Oct 20 '06 #5

P: n/a
PS
"Steve" <cc******@bigpond.net.auwrote in message
news:So*******************@news-server.bigpond.net.au...
>
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

Oct 20 '06 #6

P: n/a
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

Oct 21 '06 #7

P: n/a
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.

--
Dave Sexton

"Truong Hong Thi" <th*****@gmail.comwrote in message
news:11**********************@f16g2000cwb.googlegr oups.com...
>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

Oct 21 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.