- Root base class implements IDisposable interface
- Add a finalizer as a defensive mechanism (this will keep resources longer in memory if Dispose is not called but will dispose of resources in time)
- Both Dispose and finalizer should be virtual methods for derived classes to override
- Derived class must have a finalizer as a defensive mechanism (this will keep resources longer in memory if Dispose is not called but will dispose of resources in time)
- Implement IDisposable, which requires you to implement Dispose method, which does the following:
- frees unmanaged resources
- frees all managed resources
- sets flag indicating that the object has been disposed
- suppress finalization
- call base class to clean up all of its resources
- this can be done by implementing virtual Dispose(bool) method that allows factoring out common tasks between Dispose and finalize and call the base classes
- only release resources in Dispose and finalize methods. Do not do anything else in those methods since you may unintentionally "revive" the object again, preventing it from being garbage collected.
{
private bool _alreadyDisposed = false;
~Base()
{
Dispose(false);
}
public int Age { get; set; }
#region IDisposable Members
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool isDisposing)
{
if (_alreadyDisposed)
return;
if (isDisposing)
{
//free managed resources
}
//free unmanaged resources
_alreadyDisposed = true;
}
#endregion
}
class Derived :Base
{
private bool _disposed = false;
public int Time { get; set; }
public void PrintStuff()
{
Console.WriteLine(this.GetType());
}
~Derived()
{
Dispose(false);
}
protected virtual void Dispose(bool isDisposing)
{
if (_disposed)
return;
if (isDisposing)
{
//free managed resources
}
//free unmanaged resources
//call base class here
base.Dispose(isDisposing);
_disposed = true;
}
}
No comments:
Post a Comment