PC Review


Reply
Thread Tools Rate Thread

Destructor not being called - why?

 
 
renegade_master_12121@yahoo.co.uk
Guest
Posts: n/a
 
      24th Apr 2007
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.

 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      24th Apr 2007
At a guess (without testing):

When the form is closed, it is also Dispose()d (pehaps by Close()
itself, perhaps by Application.Run()). The normal pattern for
Dispose() on a class with a finalizer is to tell the garbage collector
(GC) that we are clean, and don't need finalizing. Hence, Dispose()
gets called, but the finalizer does not.

Perhaps override Dispose(bool disposing); if disposing is true, then
you are being called via Dispose() and you can still talk to any
undisposed objects you can see. If disposing is false you are being
called via the finalizer, and you shouldn't attempt to talk to any
managed objects (since they may be in an indeterminate state); you
should just release any unmanaged resources you have (e.g. windows
handles, native files, etc).

Marc


 
Reply With Quote
 
Alex Meleta
Guest
Posts: n/a
 
      24th Apr 2007
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: (E-Mail Removed)
[private.php?do=newpm&u=]
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.

 
Reply With Quote
 
Andy
Guest
Posts: n/a
 
      24th Apr 2007
On Apr 24, 6:45 am, "Marc Gravell" <marc.grav...@gmail.com> wrote:
> At a guess (without testing):
>
> When the form is closed, it is also Dispose()d (pehaps by Close()
> itself, perhaps by Application.Run()). The normal pattern for
> Dispose() on a class with a finalizer is to tell the garbage collector
> (GC) that we are clean, and don't need finalizing. Hence, Dispose()
> gets called, but the finalizer does not.


Also, I don't think there is any guarantee that finalizes will be
called at all.

 
Reply With Quote
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      24th Apr 2007
"Andy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Apr 24, 6:45 am, "Marc Gravell" <marc.grav...@gmail.com> wrote:
>> At a guess (without testing):
>>
>> When the form is closed, it is also Dispose()d (pehaps by Close()
>> itself, perhaps by Application.Run()). The normal pattern for
>> Dispose() on a class with a finalizer is to tell the garbage collector
>> (GC) that we are clean, and don't need finalizing. Hence, Dispose()
>> gets called, but the finalizer does not.

>
> Also, I don't think there is any guarantee that finalizes will be
> called at all.
>



It's not because it's not guaranteed 100% that they aren't called most of
the time, chances that the finalizer doesn't run is extremely low.

Willy.

 
Reply With Quote
 
renegade_master_12121@yahoo.co.uk
Guest
Posts: n/a
 
      26th Apr 2007
On Apr 24, 1:44 pm, Andy <a...@med-associates.com> wrote:
> On Apr 24, 6:45 am, "Marc Gravell" <marc.grav...@gmail.com> wrote:
>
> > At a guess (without testing):

>
> > When the form is closed, it is also Dispose()d (pehaps by Close()
> > itself, perhaps by Application.Run()). The normal pattern for
> > Dispose() on a class with a finalizer is to tell the garbage collector
> > (GC) that we are clean, and don't need finalizing. Hence, Dispose()
> > gets called, but the finalizer does not.

>
> Also, I don't think there is any guarantee that finalizes will be
> called at all.


OK, thanks, I need to look more at Finalize() and Dispose()

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Destructor is not called Eitan Microsoft C# .NET 4 16th Jan 2008 02:05 AM
destructor not being called Andy Fish Microsoft C# .NET 7 2nd Oct 2007 12:52 AM
Destructor: not gauranteed to be called? Peter Oliphant Microsoft VC .NET 35 3rd Feb 2006 09:45 AM
Winform destructor not being called Martin Hart - Memory Soft, S.L. Microsoft Dot NET Framework 3 20th Nov 2004 03:59 PM
C# destructor called, then a method Jose Microsoft C# .NET 1 7th May 2004 08:02 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:34 AM.