4

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?

8 Answers8

4

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

   using(var some = new Something())
   {
    //use normally
   }
eglasius
  • 35,831
  • 5
  • 65
  • 110
3

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

  • The problem with this is that if `obj` is of a type that implements `IDisposable` [explicitly](http://msdn.microsoft.com/en-us/library/ms173157.aspx), then intellisense might not find any method with the above technique. You would then have to type `IDisposable d = obj;` and see if it gives an error or not. – Jeppe Stig Nielsen Sep 05 '12 at 13:57
  • It's correct. If saying `using (obj) { }` builds without error, then the compile-time type of `obj` implements `IDisposable`. And in that case you *should* use `using`. Most of the time it is then better to put a local variable assignment in the `()` brackets, like `using (var obj = someMethodCallOrNewObjectExpression()) { ... }`. – Jeppe Stig Nielsen Sep 06 '12 at 06:01
2

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
  • 687,336
  • 108
  • 737
  • 1,005
0

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

supercat
  • 77,689
  • 9
  • 166
  • 211
0

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
  • 27,773
  • 7
  • 53
  • 49
0

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
  • 19,721
  • 4
  • 47
  • 52
0

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
  • 29,221
  • 14
  • 67
  • 78