[.net] Dispose pattern for .Net Applications

Started by
3 comments, last by Fiddler 14 years ago
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]
Cheers,Eliza
Advertisement
arrggh, this hurts my eyes!
Please use
  </pre> or <!--STARTSCRIPT--><!--source lang="cpp"--><div class="source"><pre> [ source ] <br></pre></div><!--ENDSCRIPT--> tags
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
And change

public override void Dispose(bool doingDispose)

to

protected override void Dispose(bool doingDispose)
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?
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.

[OpenTK: C# OpenGL 4.4, OpenGL ES 3.0 and OpenAL 1.1. Now with Linux/KMS support!]

This topic is closed to new replies.

Advertisement