I need some advice on the implementation of the Dispose
method.
In our application the user designs their own UI. I have a preview window that shows what t
Based on the types of objects that you list (i.e. Database, WebBrowser, Bitmap, etc.) these are only managed resources as far as .NET is concerned. Thus, you should implement IDisposable
on any class that has disposable types as members. If they are locally declared instances, you just call 'using()' on them. While these instances you mention do have unmanaged resources under them, this is abstracted away from you by .NET thru the types you are using. Since you are only using managed types, you should implement IDisposable
but without a finalizer. You only need to implement a finalizer if you truly have unmanaged resources as class members.
It seems that you are confusing inheritance (is a) with aggregation/containment (has a). For example, if "Container" contains a disposable resource as a class member, it is called aggregation/containment. So, calling base.Dispose()
in the IDisposable
implementation of Container has nothing to do with disposing of the disposable resource inside of Container. You should remember that if a class derives from Container, say "DerivedContainer", that it is an instance of Container albeit with additional members and/or functionality. So any instance of "DerivedContainer" has all of the members that its base class "Container" does. If you never called base.Dispose()
, the disposable resource in "Container" would not be released properly (it would actually by the GC, but it's bad practice for many reasons to just 'let .NET take care of it') - please refer to my posted answer at Is it bad practice to depend on the .NET automated garbage collector?.
If you didn't call the base class Dispose()
, you'd end up with a partially disposed object (disposed in the derived class but not in the base class) - a very bad scenario. So it is very important to call the base class Dispose()
.
I have a best practices pattern I've developed (with lots of experience and debugging memory dumps) written on my blog as an example. It shows how to implement IDisposable
on a base class as well as a derived class:
http://dave-black.blogspot.com/2011/03/how-do-you-properly-implement.html
I Implement IDisposable
class ConnectionConfiguration:IDisposable
{
private static volatile IConnection _rbMqconnection;
private static readonly object ConnectionLock = new object();
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposing)
{
return;
}
if (_rbMqconnection == null)
{
return;
}
lock (ConnectionLock)
{
if (_rbMqconnection == null)
{
return;
}
_rbMqconnection?.Dispose();//double check
_rbMqconnection = null;
}
}
}
Question 1: Implement IDisposable
as well, using the following pattern:
public class MyClass : IDisposable
{
bool disposed;
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
//dispose managed resources
}
}
//dispose unmanaged resources
disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
Question 2: What Microsoft means is that a derived class calls dispose on it's parent class. The owner of the instance only calls Dispose on the most derived type.
An (shortened) example:
class Parent : IDisposable
{
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
//dispose managed resources
}
}
//dispose unmanaged resources
disposed = true;
}
}
class Child : Parent, IDisposable
{
protected override void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
//dispose managed resources
}
base.Dispose(disposing);
}
//dispose unmanaged resources
disposed = true;
}
}
class Owner:IDisposable
{
Child child = new Child();
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
if(child!=null)
{
child.Dispose();
}
}
}
//dispose unmanaged ressources
disposed = true;
}
}
The owner only calls Dispose
on the Child, but not on the Parent. The Child is responsible for calling Dispose
on the Parent.