Recently, I was researching some tricky bugs about object not disposed.
I found some pattern in code. It is reported that some m_foo is not disposed, while it seems all instances of SomeClass has been disposed.
public class SomeClass: IDisposable
{
void Dispose()
{
if (m_foo != null)
{
m_foo.Dispose();
}
if (m_bar != null)
{
m_bar.Dispose();
}
}
private Foo m_foo;
private Bar m_bar;
}
I suspects that Foo.Dispose might throw a exception, so that following code is not executed so m_bar is not disposed.
Since Foo/Bar might be from third party, so it is not guaranteed to not throwing exception.
If just wrap all Dispose invocation with try-catch, the code will turn to be clumsy.
What's best practice to handle this?