We all know the System.IDisposable pattern. It's been described a zillion time, also here on StackOverflow:
link: Dispose() for cleaning up managed resources?
The Disposable patterns advises that I should only dispose managed resources if my object is being disposed, not during finalize
You can see that this happens because the following code is advised:
protected void Dispose(bool disposing)
{
if (disposing)
{
// Code to dispose the managed resources of the class
}
// Code to dispose the un-managed resources of the class
}
I know that my class should implement System.IDisposable whenever my class has a (private) member that implements System.IDisposable. The Dispose(bool) should call the Dispose() of the private member if the boolean disposing is true.
Why would it be a problem if the Dispose would be called during a finalize? So why would the following Dispose be a problem if it is called during finalize?
protected void Dispose(bool disposing)
{
if (myDisposableObject != null)
{
myDisposableObject.Dispose();
myDisposableObject = null;
}
}
When an object's finalizer runs, one of the following will be true of almost any IDisposable
object it might hold:
It held the only reference to that other object, and that other object's finalizer has already run, so there's no need to do anything with it.
It held the only reference to that other object, and that other object's finalizer is scheduled to run, even though it hasn't yet, and there's no need to do anything with it.
Other objects are still using the IDisposable
object, in which case the finalizer must not call Dispose
.
The other object's Dispose
method cannot be safely run from a finalizer threading context (or, more generally, any threading context other than the one where the object was created), in which case the finalizer must not call Dispose
.
Even in the cases where none of the above would apply, code which knows that will probably know a lot about its cleanup requirements beyond the fact that it implements IDisposable
, and should often use a more detailed cleanup protocol.
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