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 / .NET Framework / New Users / December 2004

Tip: Looking for answers? Try searching our database.

Destructor - ??

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Christopher - 07 Dec 2004 10:45 GMT
In my C# code there is a Destructor. I placed some code into it. But it never
gets called. I tried many times, can anyone suggest me what to do.
Thanks in advance
Signature

Christopher

Kieran Lynam - 07 Dec 2004 11:26 GMT
You're destructor will get executed - just don't when! Destructors in
.NET are "non-deterministic", meaning you can't predict when they will
be executed. This is because they are called by the garbage collector
when it cleans up your object.

In fact, using destructors as your primary mechanism for doing cleanup
is NOT recommended. It's best practice to employ something called the
"IDisposable" pattern:

http://msdn.microsoft.com/msdntv/episode.aspx?xml=episodes/en/20030501CLRBA/mani
fest.xml


> In my C# code there is a Destructor. I placed some code into it. But it never
> gets called. I tried many times, can anyone suggest me what to do.
> Thanks in advance
Richard Blewett [DevelopMentor] - 07 Dec 2004 12:10 GMT
In fact your destuctor (or finalizer in .NET speak) may not even get executed at all, at procss shutdown the runtime will only wait two seconds for all finalizers to run before it calles TerminateProcess().

This is one of the reasons I wish they had used a syntax other than destructor for a finalizer in C#. C++/CLI the successor to Managed C++ in teh next version intrduces the following syntax IIRC

class Foo
{
  ~Foo() {}  // this generates an implementation of IDisposable
  !Foo() {}  // This is a finalizer
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

  You're destructor will get executed - just don't when! Destructors in
.NET are "non-deterministic", meaning you can't predict when they will
be executed. This is because they are called by the garbage collector
when it cleans up your object.

In fact, using destructors as your primary mechanism for doing cleanup
is NOT recommended. It's best practice to employ something called the
"IDisposable" pattern:
cody - 07 Dec 2004 14:40 GMT
>  This is one of the reasons I wish they had used a syntax other than destructor for a finalizer in C#. C++/CLI the successor to Managed C++ in
teh next version intrduces the following syntax IIRC

>  class Foo
>  {
>    ~Foo() {}  // this generates an implementation of IDisposable
>    !Foo() {}  // This is a finalizer
>  }

This is stupid - why not simply

protected void override Finalize(){}

?

Finalizers are used very rarely, why don't they optimize the syntax
verbosenes not in other places?

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu  || http://www.deutronium.tk
Richard Blewett [DevelopMentor] - 07 Dec 2004 23:55 GMT
Because Microsoft don;t trust you to write

base.Finalize();

in your override - thats why they force a different syntax and the compiler emits the call to base.Finalize() on commpilation.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

  > This is one of the reasons I wish they had used a syntax other than
destructor for a finalizer in C#. C++/CLI the successor to Managed C++ in
teh next version intrduces the following syntax IIRC

> class Foo
> {
> ~Foo() {} // this generates an implementation of IDisposable
> !Foo() {} // This is a finalizer
> }

This is stupid - why not simply

protected void override Finalize(){}

?

Finalizers are used very rarely, why don't they optimize the syntax
verbosenes not in other places?
Jon Skeet [C# MVP] - 07 Dec 2004 13:05 GMT
> In my C# code there is a Destructor. I placed some code into it. But it never
> gets called. I tried many times, can anyone suggest me what to do.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Note that finalizers should be used very sparingly - they have
performance implications, and should also not be relied upon to be
called in a timely manner (or even at all, in certain circumstances).

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Keith Patrick - 07 Dec 2004 16:52 GMT
Also, if you are cleaning something up with that code, you want to make sure
you've got it in a Finally block, as your code could be getting interrupted
by an exception you can't control (i.e. ThreadAbortException)

> In my C# code there is a Destructor. I placed some code into it. But it
> never
> gets called. I tried many times, can anyone suggest me what to do.
> Thanks in advance

Rate this thread:







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.