Destructor - ??

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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
 
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:
 
Christopher said:
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).
 
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?
 
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)
 
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?
 
Back
Top