3

Possible Duplicate:
Who should call Dispose on IDisposable objects when passed into another object?

Say you have a class with an interface like this:

public interface Foo
{
    Load(IDisposable something);
}

When implementing this method, should I call dispose when done? In other words, when a method of a class takes a Stream, Reader or anything else which is IDisposable, should this method dispose of the stream as well, or should this be left to whoever called the method?

I know either way would work, just curious what others more experienced would consider good conduct :-)

Community
  • 1
  • 1
Svish
  • 152,914
  • 173
  • 462
  • 620

4 Answers4

3

You should not call Dispose in Load method, because you might do more with IDisposable object. using should use outside to dispose if you don't need more. Sample code:

using (var something = new Something())
{
    IFoo foo = new Foo();
    foo.Load(something);

    // Do more with something
}
cuongle
  • 74,024
  • 28
  • 151
  • 206
0

It depends on the documented behaviour of your interface.

If it makes sense for the stream to stay open for '(re)use' by the caller, then you might leave it open in some documented state (e.g. with the current position after such and such block).

Otherwise, the most helpful thing to do seems to be to dispose of it for the user.

Note that the CLR framework classes sometimes have specialized overloads to allow the caller to specify what should happen. See e.g.: http://msdn.microsoft.com/en-us/library/gg712952.aspx

public StreamReader(
    Stream stream,
    Encoding encoding,
    bool detectEncodingFromByteOrderMarks,
    int bufferSize,
    bool leaveOpen
)
sehe
  • 374,641
  • 47
  • 450
  • 633
0

I would add a parameter, so the caller can tell me whether to dispose it or not (I also added void for the method return type):

public interface Foo 
{ 
    void Load(IDisposable something, bool disposeSomething); 
} 
phoog
  • 42,068
  • 6
  • 79
  • 117
  • This is not particularly common practice. Usually it makes sense in context to dispose of the parameter or not (when unsure, usually erring on the side of "not"). If you want to leave it up to the caller to decide you might as well just not dispose of it, and let them do the dispose right after your method call. – Servy Sep 07 '12 at 14:06
  • @Servy true, but forcing the developer to pass a boolean means that I know that the developer knows whether or not this method will dispose the object. If I put that information only in the documentation, I force the developer to read the documentation. – phoog Sep 07 '12 at 14:16
0

If your object contains other objects that are themselves disposable, then your object should be disposable as well.For example, an object that holds a reference to a file opened with exclusive read/write privileges should be disposable so that the client of the object can control when the underlying resource is closed or cleaned up. Make Foo interface inherit from IDisposable

lex87
  • 1,276
  • 1
  • 15
  • 33
  • This is only applicable if the `Foo` object will hold onto a reference to the `IDisposable` object after the end of the method. If it only ever uses it in the context of that one method then this doesn't apply. – Servy Sep 07 '12 at 14:07