IDisposable

T

Tony Johansson

Hi!

We know that CLR is handling all the resources that you have allocated
dynamically such as for example when you
create an ArrayList like this
ArrayList myList = new ArrayList();

What does it mean with this sentence below.
IDisposable interface to explicitly release unmanaged resources.
What is bother me most is the word unmanaged. I mean for example as I just
mentioned
an ArrayList is as I do believe a managed resource.

Tony
 
F

Family Tree Mike

Hi!

We know that CLR is handling all the resources that you have allocated
dynamically such as for example when you
create an ArrayList like this
ArrayList myList = new ArrayList();

What does it mean with this sentence below.
IDisposable interface to explicitly release unmanaged resources.
What is bother me most is the word unmanaged. I mean for example as I just
mentioned
an ArrayList is as I do believe a managed resource.

Tony

But, ArrayList does not implement IDisposable...

I prefer this sentance from the overview of the IDisposable interface on
MSDN: "Defines a method to release allocated resources."

It's a means to clean up an object. You may have managed resources in
your object which down the chain eventually contain unmanaged resources.
You clean all this up in your Dispose method.
 
P

Peter Duniho

Family said:
But, ArrayList does not implement IDisposable...

Indeed. I don't really understand Tony's question. He seems to be
asking a question about something that's not actually true.
I prefer this sentance from the overview of the IDisposable interface on
MSDN: "Defines a method to release allocated resources."

It's a means to clean up an object. You may have managed resources in
your object which down the chain eventually contain unmanaged resources.
You clean all this up in your Dispose method.

That's not quite right.

Unfortunately, there's not a single definitive reference on MSDN. But
this page is pretty close:
http://msdn.microsoft.com/en-us/library/b1yfkh5e.aspx

The basic things to keep in mind:

– Objects should implement IDisposable if they allocate _unmanaged_
resources. Technically, the only "managed resource" in .NET is memory,
so if you grab a resource via any other means, you probably need to
implement IDisposable

– Objects should implement IDisposable if they _own_ managed objects
that implement IDisposable. Note that sometimes, your class will keep a
reference to an object that implements IDisposable, but doesn't own that
object. In that case, that class shouldn't dispose the object, and thus
there's no need to implement IDisposable

– Objects that allocate _unmanaged_ resources should also implement a
finalizer. This allows the garbage collector to inform them when they
are about to be released, and gives them a chance to release those
unmanaged resources even though the client code failed to call
IDisposable.Dispose().

– There's no need to call IDisposable.Dispose() on objects in your
own class's finalizer, even if your class owns the object. That's
because, assuming the ownership is being respected in the client code
(and it should be), your class will be the last reference to the
IDisposable objects, and thus the objects will also be in the finalizer
queue, and will be disposed of properly via their own finalizers. Once
the code gets to that point, it's redundant and inefficient to call
Dispose() on those objects.

All of the above is what leads to this pattern:

class DisposableClass : IDisposable
{
public void Dispose()
{
Dispose(true);
}

~DisposableClass()
{
Dispose(false);
}

protected virtual void Dispose(bool fDisposing)
{
if (fDisposing)
{
// dispose owned IDisposable objects
}


// release all _unmanaged_ resources here


// Tell the garbage collector that the finalizer
// doesn't need to run; all the disposing/releasing
// has been taken care of already.
GC.SuppressFinalize(this);
}
}

Note the Dispose(bool) overload is typically made virtual, so subclasses
can override it and not have to add new Dispose() or finalizer
implementations.

Pete
 

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