IDisposable pattern

F

Frank Oquendo

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

Chris R

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

Jon Skeet [C# MVP]

Frank Oquendo said:
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/default.asp?url=/library/en-
us/cpgenref/html/cpconfinalizedispose.asp

aka

http://tinyurl.com/2k6e
 
S

Sunny

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top