tags:

views:

306

answers:

7

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?

+1  A: 

You should dispose anything that implements IDisposable. Just wrap it on an using:

   using(var some = new Something())
   {
    //use normally
   }
eglasius
A: 

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.

chuckj
A: 

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.
Andrew Barrett
+2  A: 

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:

  1. Dig deep in the documentation to find out why the class implements IDisposable, and if you really need to call Dispose.

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

Guffa
A: 

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.

Greg Dean
+1  A: 

An easy way would be to type obj.disp and see if intellisense has a dispose method.

acidzombie24