473,508 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# destructor and finalizer - guarentees?

I remember reading a few years back about destructors not being
guarenteed to be run. Doing a search all I can find is a reference to
some bugs with finalizers and appdomains in 1.0 and 1.1. Are
destructors guarenteed to be called in every case? Are there any
pathological cases?

Oct 13 '06 #1
5 1892
<ts******@gmail.comwrote:
I remember reading a few years back about destructors not being
guarenteed to be run. Doing a search all I can find is a reference to
some bugs with finalizers and appdomains in 1.0 and 1.1. Are
destructors guarenteed to be called in every case? Are there any
pathological cases?
If you've got some finalizers which take a long time to run, others may
not get run as there's a time limit for running finalizers at the end
of an application process.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 13 '06 #2
A finalizer may not run if an exception is thrown in the finalizer list.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Oct 13 '06 #3
ts******@gmail.com wrote:
I remember reading a few years back about destructors not being
guarenteed to be run. Doing a search all I can find is a reference to
some bugs with finalizers and appdomains in 1.0 and 1.1. Are
destructors guarenteed to be called in every case? Are there any
pathological cases?
Finalizers are not guaranteed to run, and it's not a bug.

There is no destructors in .NET. There are no reference counters.
Nothing happens when the last reference to an object goes out of scope.
The object will just stay in memory until the garbage collector collects it.

When the garbage collector finds an object that is not used and it has a
finalizer, it will be placed in the finalizing queue, where the
finalizer eventually will be called by a background thread.

If an object has not been collected when the application ends, or if
it's still in the finalizing queue, the finalizer will never run.
Oct 14 '06 #4
Yes, the C# syntax for finalizers is the old destructor syntax in C++.
Are the caveats for this documented on MSDN anywhere? I failed to find
a section discussing this.

Göran Andersson wrote:
ts******@gmail.com wrote:
I remember reading a few years back about destructors not being
guarenteed to be run. Doing a search all I can find is a reference to
some bugs with finalizers and appdomains in 1.0 and 1.1. Are
destructors guarenteed to be called in every case? Are there any
pathological cases?

Finalizers are not guaranteed to run, and it's not a bug.

There is no destructors in .NET. There are no reference counters.
Nothing happens when the last reference to an object goes out of scope.
The object will just stay in memory until the garbage collector collects it.

When the garbage collector finds an object that is not used and it has a
finalizer, it will be placed in the finalizing queue, where the
finalizer eventually will be called by a background thread.

If an object has not been collected when the application ends, or if
it's still in the finalizing queue, the finalizer will never run.
Oct 16 '06 #5
Look up Garbage collection. There you will find the section "Developer
Backgrounds in Memory Management" that discusses the difference in
memory management between different platforms. You will also find other
information about memory management.

ts******@gmail.com wrote:
Yes, the C# syntax for finalizers is the old destructor syntax in C++.
Are the caveats for this documented on MSDN anywhere? I failed to find
a section discussing this.

Göran Andersson wrote:
>ts******@gmail.com wrote:
>>I remember reading a few years back about destructors not being
guarenteed to be run. Doing a search all I can find is a reference to
some bugs with finalizers and appdomains in 1.0 and 1.1. Are
destructors guarenteed to be called in every case? Are there any
pathological cases?
Finalizers are not guaranteed to run, and it's not a bug.

There is no destructors in .NET. There are no reference counters.
Nothing happens when the last reference to an object goes out of scope.
The object will just stay in memory until the garbage collector collects it.

When the garbage collector finds an object that is not used and it has a
finalizer, it will be placed in the finalizing queue, where the
finalizer eventually will be called by a background thread.

If an object has not been collected when the application ends, or if
it's still in the finalizing queue, the finalizer will never run.
Oct 19 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
5261
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our...
4
3790
by: Joe | last post by:
I am looking for the quintessential blueprint for how a C++ like destructor should be implemented in C#. I see all kinds of articles in print and on the web, but I see lots of discrepencies. For...
6
3794
by: Jim H | last post by:
I have a class object that creates and uses a System.Threading.Timer. In the destructor for my class I cancel the timer using timer.Change(infinite, infinite) then I call timer.Dispose() to clean...
11
2809
by: Sharon | last post by:
I have heard that if I add a constructor it is not good, it complicates things and that it is better to use the Dispose. Can anybody explain this for me? -- Regards Sharon G.
3
2222
by: esafran | last post by:
I've defined a class, but now I want to Define a Finalizer (destructor)... How do I call the Base Finalizer???, void Finalize() is a protected override method and Type.GetType Does not work. ...
35
3271
by: Peter Oliphant | last post by:
I'm programming in VS C++.NET 2005 using cli:/pure syntax. In my code I have a class derived from Form that creates an instance of one of my custom classes via gcnew and stores the pointer in a...
3
2448
by: RitualDave | last post by:
This compiles and runs successfully in VS2005: ref class A { private: ~A() { this->!A(); } // private! !A() { } // private! }; ....
54
5135
by: Zytan | last post by:
I have a log class that makes a synchronized TextWriter like so, in the constructor: StreamWriter sw = new StreamWriter(filename); tw = TextWriter.Synchronized(sw); In the destructor,...
2
2538
by: Scott McFadden | last post by:
I have a C++ CLI Managed DLL project which is consumed by C# clients. What is the recommended way for ensuring proper clean up of managed resources and native resources in a managed C++ class? ...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7382
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7042
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7495
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5052
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3181
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1556
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.