473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

*Dependent* Unmanaged Resources, Dispose and finalizers

Hi People,

Consider the following:

class A : IDisposable
{
public A ( Stream aResource ) { mResource = aResource ; }

public void Dispose()
{
if ( !mDisposed )
{
Dispose(true);
GC.SuppressFinalize(this);
}
}

protected virtual void Dispose( bool aDisposing )
{
if ( aDisposing )
{
mResource.Dispose();
}
mDisposed = true ;
}

~A() { Dispose(false); }

bool mDisposed = false ;
Stream mResource = null ;
}

AFAICT I can only call mResource.Dispose() in the "Disposing" branch.
That means that for this particular class, its finalizer won't (and can't)
do anything.
So, is it correct to say that this class needs Dispose but not the
finalizer?
Or more generally, are the following guidelines correct?

* Implement a finalizer if the class itself has unmanaged resources
* Implement Dispose if the class OR ANY DEPENDENT OBJECTS has unmanaged
resources.
* Use the Dispose pattern ONLY if you need to implement BOTH (don't use it
if you just need Dispose)

If the above is correct, the following is the correct implementation for
Dispose in this class:

class A : IDisposable
{
public A ( Stream aResource ) { mResource = aResource ; }

public void Dispose()
{
if ( !mDisposed )
{
mResource.Dispose();
mDisposed = true ;
}
}

bool mDisposed = false ;
Stream mResource = null ;
}
Some readers may not know this but finalizers are expensive (their force the
object to live up to the last GC generation and even then are totally free
only after an _additonal_ GC collection). You don't want to add one unless
strictly required.
TIA

Fernando Cacciola
Nov 17 '05 #1
0 1194

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

Similar topics

2
by: Marek Malowidzki | last post by:
Hi all, I am writing a component that exposes a C++ library as a .NET component. The approach is somewhat automatic: every library C++ class has its managed C++ counterpart that keeps a pointer...
6
by: Mark Broadbent | last post by:
Everything I have read suggests that if I open files or database connections or the like, that I should explicitly close those said resources before making the object subject to GC. This is...
5
by: Barry Anderberg | last post by:
I'm using a tool by Sci-Tech called the .NET Memory Profiler. We have a massive .NET/C# application here and it has been exhibiting memory leak behavior for some time. Attempting to remedy the...
6
by: Eric | last post by:
for example: SqlConnection is used in my project, how can I know if all connections were closed after open and execution. If some guys forget to close connections after using, how can i check it...
4
by: Rachel Suddeth | last post by:
What is the difference between a managed/unmanaged resource, and how do you tell which is which? I'm trying to understand how to write some Dispose() methods, and we are supposed to put code that...
156
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created...
2
by: nij | last post by:
Hi, I am developing a C# control for my own use that will be used lots of times on a form. It's inherited direct from Control, so I am doing all the drawing myself... and as there are lots of...
4
by: PromisedOyster | last post by:
There are various contradictory newsgroup postings on this issue, but I would really like a definitive answer from the .NET gurus out there? We have various WinForms that contain multiple Icons...
3
by: DaTurk | last post by:
Hi, I'm implementing the Idisposable pattern and am wondering what the difference it between managed, and unmanaged resources? http://msdn2.microsoft.com/en-us/library/fs2xkftw.aspx in the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.