Is Dispose called on parent classes?

  • Thread starter Thread starter steve bull
  • Start date Start date
S

steve bull

If I have two classes both of which have unmanaged resources that need to be cleaned up. For example class B is a child of class A
and I create an instance of class B. Then, if I call B.Dispose - Does A.Dispose get called automatically or do I have to clean up
the unmanaged resource in class A as well in B.Dispose?

Thanks,

Steve
 
Steve,

No, the pattern is typically if Dispose is called on an object, and that
object has references to other objects that implement IDisposable, then it
disposes of those objects.

If you have a reference to an implementation of IDisposable and call it,
and another class has a reference to it, that parent class will not be (nor
should it be) disposed of.

Hope this helps.
 
B.Dispose overrides A.Dispose, so it is B.Dispose's responsibilty to
call:

base.Dispose();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top