Destructor is not called

E

Eitan

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
 
S

Scott M.

..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
 
M

Michael C

Scott M. said:
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
 
S

Scott M.

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
 
E

Eitan

Scott & Michael,
Thanks
Eitan

Michael C said:
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
 

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