views:

331

answers:

11

Should I call .Dispose() after returning an object that implements IDisposable?

myDisposableObject Gimme() {
  //Code
  return disposableResult;
  disposableResult.Dispose();
}

In other words, is the object I return a copy, or is it the object itself? Thanks :)

+2  A: 

The .Dispose() will never be reached anyway.

Edit: In my opinion, no, you shouldn't. You'd destroy the Object with that.

Bobby
+3  A: 

If an object you are using implements IDisposable, you should construct and use in within a using statement - this will make sure it gets disposed of properly:

using(var mydisposableObject = new Gimme())
{
   // code
}

The way your code is constructed, you are returning the disposable object, so the call to Dispose will never be reached.

Oded
I meant this inside the function's body, not outside.
Camilo Martin
@Camilo Martin - No point in doing that as the call to `Dispose` will never be reached. You should wrap the call to this function with a `using` statement so `Dispose` gets called properly when the object goes out of scope.
Oded
+3  A: 

disposableResult.Dispose(); will never run, it is unreachable code as it will always return the line before. Wrap the method call in a using staement and dispose of the object that way.
e.g.

using (DisposeableObject MyDisposableObject = gimme())
{
//code.
}
Ben Robinson
Your code will lead to compile error, because you used keyword `object` to name a variable :P
prostynick
Yeah also i don't think the "...code." section is valid syntax either ;-) I changed it anyway.
Ben Robinson
@prostynick: It is just example code, no need to quibble.
AMissico
Usually, //... is what you use.
AMissico
All better now ;-)
Ben Robinson
+6  A: 

It's the object itself. Don't call Dispose here, even if you reverse the order so that it gets called.

Marcelo Cantos
Thanks, concise answer.
Camilo Martin
+4  A: 

No, you shouldn't. You return a reference to the object, so there is no copy made. In .NET objects are never copied unless you specifically ask for it.

Also, you can't dispose the object with code like that even if there was a situation where you should. The code after the return statement will never be executed, and you will get a warning about unreachable code.

Guffa
+1  A: 

This line: disposableResult.Dispose(); will not be executed. The returned "thing" is not a copy of object. It's a reference to object, so caller will manipulate on object created in Gimme and he (caller) should remember to dispose the object.

prostynick
A: 

You could wrap your code in a try/finally block

    try{
            int a = 0;
            return;
    }
    finally{
            //Code here will be called after you return
    }
James
+1  A: 

If you return the object, you should not dispose it before you return. It has to be up to the caller to dispose it.

erikkallen
A: 

myDisposableObject disposableResult = new myDisposableObject();

Here disposableResult is a reference to the new object created. So when you return that reference to the calling method, that reference is still pointing to the created object in the heap. Hence you can safely dispose it in the calling method.

Veer
A: 

You cannot have .Dispose() in the method which is returned.The caller should implement that.

renjucool
+1  A: 

One thing that none of the answers so far have mentioned is that you should dispose the object if Gimme throws an exception. For example:

MyDisposableObject Gimme() 
{
    MyDisposableObject disposableResult = null;
    try
    {
        MyDisposableObject disposableResult = ...

        // ... Code to prepare disposableResult

        return disposableResult;
    }
    catch(Exception)
    {
        if (disposableResult != null) disposableResult.Dispose();
        throw;
    }
}
Joe
+1, Interesting, didn't know that. I'll start doing that with exception-prone objects.
Camilo Martin