How do I know when I need to dispose of an object?

HOW do I know when I need to dispose of something? Someone just mentioned that I had several objects in my code that I needed to recycle. I had no idea that I needed to manage anything (this is my first week with C #). How do I know when I need to dispose of an object? I used http://msdn.microsoft.com/en-us/library/system.security.cryptography.hashalgorithm.aspx , and I do not see any mention of recycling on the page or did not mention it in any other objects. I was told that I (by someone on SO).

I know what I need when something inherits IDisposable, but HOW DO I KNOW when it inherits it?

+4
source share
8 answers

You must manage everything that implements IDisposable. Just wrap it when using:

using(var some = new Something()) { //use normally } 
+4
source

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

+3
source

The class implements the IDisposable interface, which means that it has a Dispose method.

Not every class that implements IDisposable requires a call to Dispose , but most do. If you see that the class implements IDisposable (or has the Dispose method, because it inherits the interface from the base class), you have two choices:

  • Go through the documentation to find out why the class implements IDisposable , and if you really need to call Dispose .

  • Just call Dispose .

Any method is safe. If the Dispose method does nothing, the call will be very fast. You can even call Dispose more than once without harm.

Moreover, just calling the Dispose method is to use the using block:

 using (FileStream s = File.OpenRead(path)) { ... } 

At the end of a block bracket, the Dispose method is called automatically. The using block is implemented as try...finally , so the Dispose method is guaranteed to be called even if an exception is thrown in the block.

+2
source

If a class implements IDisposable, you should be in control of the intentions of that class. If it is not, do not do it. In this case, the HashAlgorithm comes from ICryptoTransform, which comes from IDisposable. This means that all instances of classes that are omitted from HashAlgorithm must be removed.

0
source

You must get rid of any object that implements the IDisposable interface.

 public abstract class HashAlgorithm : ICryptoTransform, IDisposable 

Anything that has unmanaged resources (such as DB connections) must implement the IDisposable interface.

There are several good reasons for this:

  • You know that unmanaged resources (which are usually pretty scarce) will be cleaned up. As a rule, they will still be cleaned up in the finalizer, but due to the way the GC needs to clean objects with the finalizers, this may take some time.
  • If you implement the standard dispose pattern , you save the GC a lot of work, since there is no need to call the finalizer.
0
source

I know what I need when something inherits IDisposable, but HOW DO I KNOW when it inherits it?

Assuming you are using Visual Studio. Usually I right-click on a type, then "Go to Definition". If I see that this or any of its superclasses implements IDisposable, I make sure that I invoke Dispose on it. This is usually done by wrapping it in a use block that others have talked about.

0
source

"Will the last leave the room, please turn off the light?"

An object that implements IDisposable contains the information and momentum necessary to perform some "cleanup" operations that should happen "sometime", but which cannot happen while the object is still in use. If the object is completely left, these cleaning operations will not occur. The system includes a custodian with which objects can be registered when they are created; if the object is left by absolutely everyone except the keeper, the keeper can ask the object to perform its cleaning actions before the custodian also leaves it. Please note that for a number of reasons, the custodian is not 100% effective in processing abandoned objects. Therefore, it is highly desirable that, if possible, the last object contain a useful link to the object, getting rid of it before abandoning the link.

0
source

All Articles