Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

Destructor is not called

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Eitan - 16 Jan 2008 00:02 GMT
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
Scott M. - 16 Jan 2008 00:18 GMT
.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

> Hello,
>
[quoted text clipped - 18 lines]
> Thanks,
> Eitan Barazani
Michael C - 16 Jan 2008 00:32 GMT
> 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
Scott M. - 16 Jan 2008 01:23 GMT
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

>> Because .NET uses Garbage Collection and non-deterministic finalization
>> for objects stored on the managed heap, you can't know when (or
[quoted text clipped - 10 lines]
>
> Michael
Eitan - 16 Jan 2008 02:05 GMT
Scott & Michael,
Thanks
Eitan

> > Because .NET uses Garbage Collection and non-deterministic finalization
> > for objects stored on the managed heap, you can't know when (or
[quoted text clipped - 9 lines]
>
> Michael

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.