0

I have a class that implements the interface IDisposable. My understanding is that when you have finished using the object (something like the line below) the Dispose method is called to make sure everything is cleaned up - is that correct?

 myObj = null;

I also would like to know if an exception is thrown whether the Dispose method still gets called? Or should I just be using a 'using' block?

mHelpMe
  • 6,336
  • 24
  • 75
  • 150
  • 1
    On the face of it, it seems you should go and read some more about `IDisposable` and implementation/usage patterns. Just doing `myObj = null` by itself doesn't call `Dispose` directly - I think you probably need a slightly better understanding of what's going on, but Stack Overflow isn't really the place for such a question. – James Thorpe Jul 13 '15 at 10:16
  • 1
    Dispose method is called "automatically" only if you are using a `using` statement – Matteo Umili Jul 13 '15 at 10:17
  • Answer to your second question: http://stackoverflow.com/questions/518352/does-dispose-still-get-called-when-exception-is-thrown-inside-of-a-using-stateme – Tim Schmelter Jul 13 '15 at 10:19
  • Setting an instance to `null` is not doing anything useful. – Tim Schmelter Jul 13 '15 at 10:26
  • 1
    If dispose is not explicitly called assuming, there's no using statement, the finalizer will come in the picture, which is standard practice for types which implement IDisposable, this would take care of the type, though it would first queue and then clean in next GC iteration, so little costly – Mrinal Kamboj Jul 13 '15 at 10:38

2 Answers2

2

My understanding is that when you have finished using the object (something like the line below) the Dispose method is called to make sure everything is cleaned up - is that correct?

No, it is not correct. Setting an instance of an object to null will not call its Dispose method, you should do that explicitly when you're done with an object.

myObj.Dispose();// Im done with myObj!

This is often done by utilizing using

using(var myObj = new MyObject())
{
     myObj.DoSomething();
} // Dispose automatically called.

The above ensures Dispose is called, even if an exception is thrown within the using block.

Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • 3
    It's worth nothing that it's not useful at all to set an instance to null. The garbage collector will notice that an object is referenced no longer anyway. [The JIT is usually smart enough to realize that obj = null can be optimized away](http://blogs.msdn.com/b/clyon/archive/2004/12/01/273144.aspx). – Tim Schmelter Jul 13 '15 at 10:29
  • Even if we are not able to call dispose explicitly, then finalizer should come in the picture for GC finalization – Mrinal Kamboj Jul 13 '15 at 10:39
1

Dispose should be explicitly called if want to release the system resources used by this object at that particular moment . If we are not calling the Dispose , the system will take care of this at some point with Garbage Collector .

If an object implementes System.IDisposable then we will be able to call the dispose method on it.

Using will be converted in to try, finally block by CLR and the dispose method will be called the in the finally block to release the resources.

Check this link for all information you required like how system handles this and all Using Statement

Just copying the code from the mentioned link to make it more useful.

If you have some code like this

using (MyResource myRes = new MyResource())
{
    myRes.DoSomething();

}

Then it get's automatically converted to

MyResource myRes= new MyResource();
try
{
    myRes.DoSomething();
}
finally
{
    // Check for a null resource.
    if (myRes!= null)
        // Call the object's Dispose method.
        ((IDisposable)myRes).Dispose();
}
Midhun Murali
  • 2,089
  • 6
  • 28
  • 49