Why Form.Dispose isn't called?

C

cody

MyDlg dlg = new MyDlg();
dlg.ShowDialog();

after the dialog is closed here I expected that Form.Dispose would be called
but this isn't the case.
Why? After closing the application, Dispose was called.

I had a very funny bug because of this: I run a System.Windows.Forms.Timer
in this dialog and expected it would be stopped & disposed after the dialog
was closed but this was not the case. It continued running after the dialog
was closed and at the end I had multiple instances of this dialog in memory
with their timers running but neither of them was visible and they did
funny things in my app.
 
J

Jon Skeet [C# MVP]

cody said:
MyDlg dlg = new MyDlg();
dlg.ShowDialog();

after the dialog is closed here I expected that Form.Dispose would be called
but this isn't the case.
Why? After closing the application, Dispose was called.

I had a very funny bug because of this: I run a System.Windows.Forms.Timer
in this dialog and expected it would be stopped & disposed after the dialog
was closed but this was not the case. It continued running after the dialog
was closed and at the end I had multiple instances of this dialog in memory
with their timers running but neither of them was visible and they did
funny things in my app.

Well, the documentation does talk about this:

<quote>
Because a form displayed as a dialog box is not closed, you must call
the Dispose method of the form when the form is no longer needed by
your application.
</quote>
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi cody,
When dilaog box (form shown via ShowDialog) is closed it hides the form
rather than dispose it. That's because it is more likely that one will want
to read some of the forms control values, etc.

When a form is show as modaless, though, calling close dipose the form.
If you want your dialog disposed you can use the following

using(SomeDialogForm dlg = new DialogForm())
{
if(dlg.ShowDialog() == DialogResult.OK)
{
//Process the dialog data
}
}
 
C

cody

Thank you both. That makes sense.

But is it correct to say that it is not very important in a WinForms based
application to call dispose after the dialog has finished because the
finalizer is called in the next but one garbage collection anyway?

Another problem arises because GC doesn't call dispose, but instead
~MyDlg(), so all code I write into the Dispose method will not invoked.
 
J

Jay B. Harlow [MVP - Outlook]

Cody,
But is it correct to say that it is not very important in a WinForms based
application to call dispose after the dialog has finished because the
finalizer is called in the next but one garbage collection anyway?
That is why it is VERY important to call Dispose on a dialog!

You have no idea how far away that garbage collection is, it could be
several minutes! You are holding onto valuable unmanaged resources (Win32
window handles) that should be released "asap", hence you should call
Dispose on your dialog "asap".

Hope this helps
Jay
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi cody,

The component's finalizer (~Component in C#) calls Dispose. Hence your
dispose implementation is going to be called by the GC
 

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