473,396 Members | 1,789 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,396 software developers and data experts.

Destructor is not called

Hello,

I have a WinForm application I am working on.

The application has one main Form with a few buttons. Pressing each of the
buttons will open a new Form. On one of these Forms (the one in question,
RunForm) I am instantiating an Object (a class I wrote and named ChannelComm)
that has a destructor.

This Form in question (RunForm) is started/called in the following way:

RunForm dlg = new RunForm();
DialogResult res = dlg.ShowDialog();
dlg.Dispose();

What I am noticing, using a breakpoint, is that the destructor for
ChannelComm is not called. Why isn’t it called?

Thanks,
Eitan Barazani
Jan 16 '08 #1
4 3267
..NET doesn't use destructors, it uses Finalizers. The main difference being
that with a destructor, you know when it is going to occur and with
Finalizers you don't.

Because .NET uses Garbage Collection and non-deterministic finalization for
objects stored on the managed heap, you can't know when (or technically
"if") an object will be removed from memory. That is the moment in which
your Finalizer will fire. Instead, you should write any cleanup code you
have in the class's Dispose() method and simply call Dispose when your code
no longer is using the object.

FYI - Decaring your instance with CSharp's "using" statement, causes that
object to automatically call its' Dispose method when the using block is
complete.

-Scott

"Eitan" <Ei***@discussions.microsoft.comwrote in message
news:4C**********************************@microsof t.com...
Hello,

I have a WinForm application I am working on.

The application has one main Form with a few buttons. Pressing each of
the
buttons will open a new Form. On one of these Forms (the one in question,
RunForm) I am instantiating an Object (a class I wrote and named
ChannelComm)
that has a destructor.

This Form in question (RunForm) is started/called in the following way:

RunForm dlg = new RunForm();
DialogResult res = dlg.ShowDialog();
dlg.Dispose();

What I am noticing, using a breakpoint, is that the destructor for
ChannelComm is not called. Why isn't it called?

Thanks,
Eitan Barazani

Jan 16 '08 #2
"Scott M." <sm**@nospam.nospamwrote in message
news:ON**************@TK2MSFTNGP04.phx.gbl...
Because .NET uses Garbage Collection and non-deterministic finalization
for objects stored on the managed heap, you can't know when (or
technically "if") an object will be removed from memory. That is the
moment in which your Finalizer will fire. Instead, you should write any
cleanup code you have in the class's Dispose() method and simply call
Dispose when your code no longer is using the object.
It should be added that you class should implement the IDisposable
interface, not just add a Dispose method. This should only be used for
objects that have a need for it. Usually that need is unmanaged resources or
the use of managed objects that use unmanaged resources (eg Pen or Brush).

Michael
Jan 16 '08 #3
And, to add upon that...

If you class isn't using unmanaged resources, but does need clean up of .NET
resources, that clean up should either occur at the end of the method that
uses the resources or in a separate cleanup type method (i.e. "close").

-Scott

"Michael C" <mi**@nospam.comwrote in message
news:ep**************@TK2MSFTNGP03.phx.gbl...
"Scott M." <sm**@nospam.nospamwrote in message
news:ON**************@TK2MSFTNGP04.phx.gbl...
>Because .NET uses Garbage Collection and non-deterministic finalization
for objects stored on the managed heap, you can't know when (or
technically "if") an object will be removed from memory. That is the
moment in which your Finalizer will fire. Instead, you should write any
cleanup code you have in the class's Dispose() method and simply call
Dispose when your code no longer is using the object.

It should be added that you class should implement the IDisposable
interface, not just add a Dispose method. This should only be used for
objects that have a need for it. Usually that need is unmanaged resources
or the use of managed objects that use unmanaged resources (eg Pen or
Brush).

Michael

Jan 16 '08 #4
Scott & Michael,
Thanks
Eitan

"Michael C" wrote:
"Scott M." <sm**@nospam.nospamwrote in message
news:ON**************@TK2MSFTNGP04.phx.gbl...
Because .NET uses Garbage Collection and non-deterministic finalization
for objects stored on the managed heap, you can't know when (or
technically "if") an object will be removed from memory. That is the
moment in which your Finalizer will fire. Instead, you should write any
cleanup code you have in the class's Dispose() method and simply call
Dispose when your code no longer is using the object.

It should be added that you class should implement the IDisposable
interface, not just add a Dispose method. This should only be used for
objects that have a need for it. Usually that need is unmanaged resources or
the use of managed objects that use unmanaged resources (eg Pen or Brush).

Michael
Jan 16 '08 #5

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

Similar topics

52
by: Newsnet Customer | last post by:
Hi, Statement 1: "A dynamically created local object will call it's destructor method when it goes out of scope when a procedure returms" Agree. Statement 2: "A dynamically created object...
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...
11
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
16
by: Timothy Madden | last post by:
Hy I have destructors that do some functional work in the program flow. The problem is destructors should only be used for clean-up, because exceptions might rise at any time, and destructors...
6
by: Squeamz | last post by:
Hello, Say I create a class ("Child") that inherits from another class ("Parent"). Parent's destructor is not virtual. Is there a way I can prevent Parent's destructor from being called when a...
11
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our...
4
by: Joe | last post by:
I am looking for the quintessential blueprint for how a C++ like destructor should be implemented in C#. I see all kinds of articles in print and on the web, but I see lots of discrepencies. For...
35
by: Peter Oliphant | last post by:
I'm programming in VS C++.NET 2005 using cli:/pure syntax. In my code I have a class derived from Form that creates an instance of one of my custom classes via gcnew and stores the pointer in a...
14
by: gurry | last post by:
Suppose there's a class A. There's another class called B which looks like this: class B { private: A a; public : B() { a.~A() } }
5
by: junw2000 | last post by:
I use the code below to study delete and destructor. #include <iostream> using namespace std; struct A { virtual ~A() { cout << "~A()" << endl; }; //LINE1 void operator delete(void* p) {...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.