I have a class that creates several IDisposable objects, all of these objects are then passed to another 'manager' class in a 3rd party library. As I need some of the objects in later calls I kept a local field reference to the created objects so that I could access them at a later time. When I ran FxCop on the class it said that I should implement IDisposable, due to the disposable objects that I kept a reference to. My questions are:
- Should I implement IDisposable for my class? (or is it the manager's responsibility?)
- If so, should I only dispose the objects I kept a reference to? or should I find a way to dispose all objects I created.
My code:
public class MyClass
{
ClassThatIsDisposable aReference;
}
public MyClass(ManagerClass manager)
{
ClassThatIsDisposable transient=new ClassThatIsDisposable();
manager.Add(transient);
aReference=new ClassThatIsDisposable();
manager.Add(aReference);
}
public void LaterCall()
{
areference.Method();
}