That is because the Component (the base class of the Form) has the
..Dispose method is looked like this:
Component.Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
The calling of System.GC::SuppressFinalize on the object disposed remove
it from the Finalization queue and Finalize method (destructor in C#)
will never be called.
Put your logging in the Dispose method and everything will work fine:
protected override void Dispose(bool disposing)
{
...
Dump("dtor");
...
}
Regards, Alex Meleta
Blog::
http://devkids.blogspot.com
-----Original Message-----
From:
re*******************@yahoo.co.uk
[mailto:re*******************@yahoo.co.uk]
Posted At: Tuesday, April 24, 2007 12:34 PM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: Destructor not being called - why?
Subject: Destructor not being called - why?
Here is a Test.
formtest.cs:
class MyForm : System.Windows.Forms.Form {
void Dump(string s){
System.IO.StreamWriter sw = new
System.IO.StreamWriter("dumpfile.txt", true);
sw.WriteLine(s, System.DateTime.Now);
sw.Close();
}
public MyForm(){
Dump("ctor");
}
~MyForm(){
Dump("dtor");
}
}
class TestForm {
static void Main(){
MyForm x = new MyForm();
System.Windows.Forms.Application.Run(x);
}
}
This file defines a form and has a Main() which creates a form and
passes it to Application.Run(). However the destructor for the form
never seems to get called; the dumpfile.txt only ever mentions the
ctor call. What am I doing wrong or misunderstanding here? Thanks.