473,385 Members | 1,400 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.

IDisposable pattern

I see a lot of this:

public void Dispose()
{
this.Dispose(true);
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
yada.yada();
GC.SupressFinalize(this);
}
}

My question is what's with the two implementations? Should you do this
all the time or only some of the time? If only some of the time, when?
What happens if disposing is false? Do you call identical code
elsewhere?

While we're here, when's the right time to code a Finalize method?

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #1
3 3304
From MS Documentation for:
IDisposable.Dispose Method

// Dispose(bool disposing) executes in two distinct scenarios.
// If disposing equals true, the method has been called directly
// or indirectly by a user's code. Managed and unmanaged resources
// can be disposed.
// If disposing equals false, the method has been called by the
// runtime from inside the finalizer and you should not reference
// other objects. Only unmanaged resources can be disposed.

Chris R.

"Frank Oquendo" <fr*******@acadx.com> wrote in message
news:uw**************@tk2msftngp13.phx.gbl...
I see a lot of this:

public void Dispose()
{
this.Dispose(true);
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
yada.yada();
GC.SupressFinalize(this);
}
}

My question is what's with the two implementations? Should you do this
all the time or only some of the time? If only some of the time, when?
What happens if disposing is false? Do you call identical code
elsewhere?

While we're here, when's the right time to code a Finalize method?

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)

Nov 15 '05 #2
Frank Oquendo <fr*******@acadx.com> wrote:
I see a lot of this:

public void Dispose()
{
this.Dispose(true);
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
yada.yada();
GC.SupressFinalize(this);
}
}

My question is what's with the two implementations? Should you do this
all the time or only some of the time? If only some of the time, when?
What happens if disposing is false? Do you call identical code
elsewhere?

While we're here, when's the right time to code a Finalize method?


Have a look at

http://msdn.microsoft.com/library/de...l=/library/en-
us/cpgenref/html/cpconfinalizedispose.asp

aka

http://tinyurl.com/2k6e

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
Hi Frank,
The resources Chris and Jon have pointed are good sources.
I'll just try to explain it in simple words.

You may, or you may not use IDisposable interface, and implement same
disposing mechanism.
The advantage if implementing of IDisposable is, that a lot of
collection classes check if the member of the collection is implementing
IDisposable and invokes the Dispose() method.

And, the purpose of the second implementation with the bool parameter is
only to signal who is calling the dispose process - user code, or
Garbage Collector. But for it to work, you need to implement finalizer
(destructor) which calls Dispose(false), so your example have to be
extended with:

~MyClass()
{
Dispose(false);
}

And, you need to signal who is calling the Dispose(bool) method, because
if it is called by the finalizer (I.e. Grabage Collector), you can not
be sure that the managed resources, referred by your class are still
alive, and you can not dispose/free them. You can free only unmanaged
resources.

Hope that helps
Sunny

P.S. Chris, Jon, please excuse me for these my interventions, but I
personally had a lot of problems understanding some terms, because of my
poor English and lack of term knowledge. Thats why I'm trying to expose
the things as I have understand them, and to explain with simple words.
So please, if somewhere (not only in this post), you find me incorrect,
or misleading, be so kind to correct me. Even if it is "technically"
correct, but completely useless :). Thanks. Sunny

In article <uw************@tk2msftngp13.phx.gbl>, fr*******@acadx.com
says...
I see a lot of this:

public void Dispose()
{
this.Dispose(true);
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
yada.yada();
GC.SupressFinalize(this);
}
}

My question is what's with the two implementations? Should you do this
all the time or only some of the time? If only some of the time, when?
What happens if disposing is false? Do you call identical code
elsewhere?

While we're here, when's the right time to code a Finalize method?

Nov 15 '05 #4

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

Similar topics

26
by: codymanix | last post by:
Last night I had several thought about RAII and want to discuss a bit. Why doesn't CSharp support destructors in structs? Wouldn't that make RAII possible like in C++? When the struct goes out of...
3
by: Ivan Neganov | last post by:
Hi, I have a custom subclass of System.IO.Stream type. I wonder how to correctly implement the IDisposable pattern in this situation. The parent Stream type apparently uses explicit interface...
5
by: Robert Heuvel | last post by:
Hi, this is what I did: public struct SWaitCursor:IDisposable { public SWaitCursor (int i) { Cursor.Current = Cursors.WaitCursor; } void System.IDisposable.Dispose() { Cursor.Current =...
3
by: Dave | last post by:
I trying to determine the best pattern for designing my business and data layers... Can the instance of the business object eventually cause memory leaks in Example 1? If your business class...
4
by: phl | last post by:
hi, My question is: 1. To avoid possible memory leaks, when you use this pattern, after you have dealth with the unmanaged resources and before you take your object off the finalize queue,...
12
by: Cordell Lawrence \(News Group\) | last post by:
There an ongoing discussion between a colleague and myself about the usefulness of the IDisposable pattern beyond the reclamation of unmanaged resources. The discussion is somewhat lengthy so I...
12
by: Mark Rae | last post by:
Hi, The following code works: HttpWebRequest objRequest = null; try { HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create("http://www.microsoft.com"); using (HttpWebResponse...
11
by: Mark Rae | last post by:
Hi, Following on from the recent thread about why HttpWebRequest doesn't implement the IDisposable interface, I got to wondering whether people make their custom classes inherit IDisposable or...
47
by: Hilton | last post by:
Hi, I'm sure I'm simplifying things here, but how about if the GC did this to objects that implement IDisposable: 1. Always Generation 1 (I think that is the correct name) 2. Get aggressive...
4
by: cgarcia0117 | last post by:
For any class I write in C# that has a member variable that implements IDisposable my class implements the IDisposable pattern. I do this to guarantee the reference to the member is explicitly...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.