Groups | Blog | Home
all groups > dotnet framework > december 2004 >

dotnet framework : Destructor - ??


Christopher
12/7/2004 2:45:08 AM
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]
12/7/2004 4:10:55 AM
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:

Keith Patrick
12/7/2004 10:52:54 AM
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)


[quoted text, click to view]

Kieran Lynam
12/7/2004 11:26:42 AM
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/manifest.xml




[quoted text, click to view]
Jon Skeet [C# MVP]
12/7/2004 1:05:32 PM
[quoted text, click to view]

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

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
cody
12/7/2004 3:40:43 PM
[quoted text, click to view]
destructor for a finalizer in C#. C++/CLI the successor to Managed C++ in
teh next version intrduces the following syntax IIRC
[quoted text, click to view]

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]
12/7/2004 3:55:11 PM
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

[quoted text, click to view]
destructor for a finalizer in C#. C++/CLI the successor to Managed C++ in
teh next version intrduces the following syntax IIRC
[quoted text, click to view]

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?
AddThis Social Bookmark Button