I have a class (myClass), which has a class member (myDisposableMem) derived from IDisposable, thus having a Dispose() method. If it was a local variable, I can use using(...) {...} to make sure Dispose() will be called on this object. But it is a class member. What is the right way to make sure the Disposed is called on the member? I can think of 2 ways:
1) add a finallize() in the class, then call myDisposableMem.Dispose() inside
or
2) make my class inherit from IDisposible:
public class myClass : IDisposable
{
...
public void Dispose()
{
myDisposableMem.Dispose();
}
}
void main ()
{
using (myClass myObj = new MyClass())
{
....
}
}
Or maybe there is a better way?
An object or piece of code is said to "own" an IDisposable if it holds a reference, and there is no reason to believe that any other object or piece of code will call Dispose on it. Code which owns an an IDisposable held in a local variable must generally, before abandoning that variable, either call Dispose on it or else hand it off to some other code or object that expects to receive ownership. An object which owns IDisposable stored in a field must generally implement IDisposable itself; its Dispose method should check if the field is null and, if not, call Dispose on it.
Note that merely holding a reference to an IDisposable does not imply that one should call Dispose on it. One should only call Dispose on an IDisposable one holds if one has a reasonable expectation that no other code is going to do so.
Objects should override Finalize only if they would could perform some cleanup in a fashion that would be both useful and safe in an unknown threading context. In general, an object should not call Dispose on other IDisposable objects within a Finalize method, because although (contrary to what some sources claim) such objects are guaranteed to exist, one of the following conditions will most likely apply:
In short, if one owns an IDisposable, one should generally figure that if it can safely be cleaned up within a finalizer thread context, it will take care of itself, and if it can't, one shouldn't try to force it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With