Part of the disposable pattern includes the following method.
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
// TODO: dispose managed state (managed objects).
}
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
// TODO: set large fields to null.
disposed = true;
}
}
This method has different handling to clean up managed and unmanaged resources. But what if I want to clean up a class member that implements IDisposable
?
Normally, I don't know if that member cleans up managed or unmanaged resources. So if I want my Dispose
method to clean up a class member that implements IDisposable
, would I call Dispose()
on that member in the managed or unmanaged sections in the code above?
You should call Dispose in the managed section (i.e., only when disposing
is true).
Part of the guidance in Dispose Pattern under Framework Design Guidelines is:
The Boolean parameter disposing indicates whether the method was invoked from the IDisposable.Dispose implementation or from the finalizer. The Dispose(bool) implementation should check the parameter before accessing other reference objects [...]. Such objects should only be accessed when the method is called from the IDisposable.Dispose implementation (when the disposing parameter is equal to true). If the method is invoked from the finalizer (disposing is false), other objects should not be accessed. The reason is that objects are finalized in an unpredictable order and so they, or any of their dependencies, might already have been finalized.
Another useful resource is Implementing a Dispose Method