A Form's Finalize Method

T

Trecius

Hello, Newsgroupians:

I've a question regarding a finalizer. Why is it that the finalize method
of a form is never being called?

I create a simple program...

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

~Form1()
{
Console.Beep();
// MessageBox.Show("Cleaning up!");
}
}

static class Program
{
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}

Why does it not beep or show a message box?

Thank you.


Trecius
 
J

Jon Skeet [C# MVP]

Trecius said:
I've a question regarding a finalizer. Why is it that the finalize method
of a form is never being called?

Why does it not beep or show a message box?

Application.Run calls Dispose on the form. Disposing the form
suppresses the finalizer.
 
M

Marc Gravell

Just a guess, but IIRC, Application.Run disposes (i.e. calls
Dispose()) the Form instance afterwards, and the Dispose() method of
many types disables the finalizer (after all, it has been tidied
already!).

Marc
 
M

Marc Gravell

Yup; Form : Component, and Component has:

public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}

which probably explains it
 
T

Trecius

Thank you, Mr. Skeet and Mr. Gravell. You're comments and answers are most
welcomed!

Trecius
 
T

Trecius

Mr. Skeet:

One more question related to my first. I'm trying to override the Dispose()
method for the form. However, when I try...

public override void Dispose()
{
...
}

I receive an error, stating it's not virtual. Of course it is not virtual,
so instead of the keyword 'override,' I use the keyword 'new.' However, I
still see that my Dispose() method is not being called. What can I do to
assure that my Dispose method is being called without having to dispose of it
manually after the Application.Run(frm)?


Thank you again.


Trecius
 
M

Marc Gravell

I receive an error, stating it's not virtual.

There is a common pattern of having a virtual Dispose(bool disposing),
which both the finalizer and Dispose call, passing false/true
respectively. Try overriding this:

protected override void Dispose(bool disposing)
{
if (disposing)
{
// regular disposal
}
base.Dispose(disposing);
}

But note that the IDE may already have done this... ;-(
 
T

Trecius

Nevermind. I forgot that the class is a partial class, so the Dispose()
method is already defined on it. I found the place where I need to add my
code. Thank you.


Trecius
 
J

Jon Skeet [C# MVP]

Trecius said:
One more question related to my first. I'm trying to override the Dispose()
method for the form. However, when I try...

public override void Dispose()
{
...
}

I receive an error, stating it's not virtual. Of course it is not virtual,
so instead of the keyword 'override,' I use the keyword 'new.' However, I
still see that my Dispose() method is not being called. What can I do to
assure that my Dispose method is being called without having to dispose of it
manually after the Application.Run(frm)?

Assuming you're using the designer, the Dispose method is already
implemented in the designer-generated partial class definition.

Could you perhaps subscribe to the Control.Disposed event for your
form?
 

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