473,387 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.

Apr 24 '07 #1
5 20409
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
Apr 24 '07 #2
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.

Apr 24 '07 #3
On Apr 24, 6:45 am, "Marc Gravell" <marc.grav...@gmail.comwrote:
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.

Apr 24 '07 #4
"Andy" <an***@med-associates.comwrote in message
news:11**********************@c18g2000prb.googlegr oups.com...
On Apr 24, 6:45 am, "Marc Gravell" <marc.grav...@gmail.comwrote:
>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.

Apr 24 '07 #5
On Apr 24, 1:44 pm, Andy <a...@med-associates.comwrote:
On Apr 24, 6:45 am, "Marc Gravell" <marc.grav...@gmail.comwrote:
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()

Apr 26 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: sahukar praveen | last post by:
Hello, This is the program that I am trying. The program executes but does not give me a desired output. ********************************************** #include <iostream.h> #include...
7
by: Douglas Peterson | last post by:
Take a look at this code, it looks funny as its written to be as short as possible: -- code -- struct Base { ~Base() { *((char*)0) = 0; } }; struct Derived : public Base
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
1
by: Jose | last post by:
Hello, We have a nunit test object. We've set breakpoints on: constructor, destructor, and a method. Constructor is called once, first. Destructor is called next. Last, the object's method...
5
by: nospam | last post by:
http://lab.msdn.microsoft.com/ProductFeedback/viewFeedback.aspx?feedbackId=FDBK16272
1
by: Karine Pinault | last post by:
I am having trouble trying to compile existing code with the /clr flag. The following error occurs and I can't figure it out how to fix this problem. I have the following declaration:...
9
by: rohits123 | last post by:
I have an overload delete operator as below ////////////////////////////////// void operator delete(void* mem,int head_type) { mmHead local_Head = CPRMemory::GetMemoryHead(head_type);...
4
by: Subra | last post by:
Hi, I am learning C++ and need export advice on the below program. I have read it that whenever a exception is genrated and control enteres the catch block it will call destructors for all the...
1
by: Jimmy Hartzell | last post by:
Specifically this function: void fill_all(GCReference<GCTestwhat_with) { for(int i = 0; i < 16; i++) { refs = what_with; } } Some destructor seems to be getting called within this function,...
4
by: Deanm007 | last post by:
The code below does not show the output when the destructor is called. It shows when the constructor is called, then it shows the age. Then it finishes. Am I doing something wrong here? Thanks ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.