How do i know when i need to dispose an object?
Question
HOW do i know when i need to dispose of something? Someone just mention i had several objects in my code that i need to dispose of. I had no idea i needed to dispose anything (this is my first week with C#). How do i know when i need to dispose an object? i was using http://msdn.microsoft.com/en-us/library/system.security.cryptography.hashalgorithm.aspx and i do not see any mention of dispose on the page or seen it mention in any other objs i was told i to dispose (by someone on SO).
I know i need to when something inherits IDisposable but HOW do i KNOW when it does inherit it?
Solution
You should dispose anything that implements IDisposable. Just wrap it on an using:
using(var some = new Something())
{
//use normally
}
OTHER TIPS
An easy way would be to type obj.disp and see if intellisense has a dispose method.
The class implements the interface IDisposable
, that means that it has a Dispose
method.
Not every class that implements IDisposable requires you to call Dispose
, but most of them do. If you see that the class implements IDisposable
(or has a Dispose
method because it inherits the interface from a base class), you have two choises:
Dig deep in the documentation to find out why the class implements
IDisposable
, and if you really need to callDispose
.Just call
Dispose
.
Either method is safe. If the Dispose
method doesn't do anything, the call will be very quick. You can even call Dispose
more than once without harm.
Even better then just calling the Dispose
method is to use a using
block:
using (FileStream s = File.OpenRead(path)) {
...
}
At the end bracket of the block the Dispose
method is called automatically. The using
block is implemented as a try...finally
, so the Dispose
method is guaranteed to be called even if an exception occurs in the block.
If an class implements IDisposable you should dispose of intances of that class. If it doesn't you don't. In this case HashAlgorithm derives from ICryptoTransform which derives from IDisposable. This means all instance of classes descending from HashAlgorithm must be disposed.
You should dispose of any object that implements the IDisposable interface.
public abstract class HashAlgorithm : ICryptoTransform,
IDisposable
Anything that has unmanaged resources (DB connections for example) should implement the IDisposable interface.
There are a couple of good reasons for this:
- You know that the unmanaged resources (which are typically quite scarce) are going to be cleaned up. Usually these will be cleared up in the finalizer anyway, but due to how the GC has to tidy up objects with finalizers this could take a while.
- If you implement the standard dispose pattern you save the GC a lot of work as it doesn't need to call the finalizer.
I know i need to when something inherits IDisposable but HOW do i KNOW when it does inherit it?
Assuming you're using Visual Studio. I usually right click on the type, then "Go To Definition". If I see that it, or any of its super classes, implement IDisposable, I make sure I call Dispose on it. This is typically done by wrapping it in a using block as others have mentioned.
"Will the last person to leave the room please turn out the lights?"
An object which implements IDisposable holds the information and impetus necessary to do some "clean-up" operations that should happen "sometime", but which can't happen while the object is still in use. If the object is totally abandoned, those clean-up operations won't happen. The system includes a custodian, with which objects can register when they are created; if an object is abandoned by absolutely everyone but the custodian, the custodian can ask the object to perform its cleanup actions before the custodian too abandons it. Note that for a variety of reasons, the custodian isn't 100% effective at handling abandoned objects. It is thus very desirable that, whenever possible, the last entity to hold a useful reference to an object dispose of it before abandoning the reference.