Sign in to follow this  
elizas

[.net] Dispose pattern for .Net Applications

Recommended Posts

elizas    100
Dispose pattern is nothing new but a better approach of disposing managed as well as unmanaged objects. This has been referred by the MSDN and meant for a better architecture towards object disposal in .Net applications. The backbone of .Net applications, the CLR, does not have any control over the unmanaged resources. It can’t dispose them automatically as it does for managed ones. Hence it needs to be done explicitly by the programmer. The proposed architecture was given by the MSDN for a flawless design and to avoid memory leaks. It provides the derived classes a chance to dispose their unmanaged (also managed) resources, if they have. Example: Snippet 1: public class BaseClass : IDisposable { bool isDisposed = false; public BaseClass() { // // TODO: Add constructor logic here // } #region IDisposable Members public void Dispose() { this.Dispose(true); GC.SuppressFinalize(this); } public virtual void Dispose(bool doingDispose) { if (!this.isDisposed) { if (doingDispose) { //Release all managed resources } //Release all Unmanaged resources over here. //So if doingDispose is FALSE, then only unmanaged resources will be released. } this.isDisposed = true; } //Destructor ~BaseClass() { this.Dispose(false); } #endregion } Snippet 2: public class DerivedClass : BaseClass { private bool isDisposed = false; public DerivedClass() { // // TODO: Add constructor logic here // } public override void Dispose(bool doingDispose) { if (!this.isDisposed) { if (doingDispose) { // Release the managed resources you added in // this derived class here. } // Release the native unmanaged resources you added // in this derived class here } } } Explanation: 1. The above example is the recommended design pattern for Disposing objects and has been implemented through out the .Net Framework. 2. Here the BaseClass has a virtual Dispose method which gives the DerivedClass a chance to dispose the unmanaged resources. 3. Again the virtual Dispose class gets executed in two scenarios with true and false parameters. 4. In normal scenario the Dispose methods gets executed with true. It happens when an instance calls the Dispose() method, which in turn calls the Dispose(true) method. 5. For false, the Dispose(false) is called only at runtime from the finalize block(destructor) and disposes only unmanaged resources. Eliza [Edited by - jpetrie on February 17, 2010 10:42:32 AM]

Share this post


Link to post
Share on other sites
Nali    131
So how should this be used?

In my case I have a "Game" object which I need to destroy/free/release every time I press "new game" button.. should I call Dispose() or Dispose(true)?

Couldn´t those 2 be combined someway?

Share this post


Link to post
Share on other sites
Fiddler    860
Dispose(bool) should be a protected method (the code in the OP is wrong). The public Dispose() method calls this.Dispose(true). The finalizer calls this.Dispose(false). Dispose() and Dispose(bool) cannot and shouldn't be combined.

To answer your question, you should call game.Dispose() to clean up your game object.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this