Dispose() for cleaning up managed resources?

See its very simple.

  1. If you are dealing with unmanaged resources - Implement both Dispose and Finalize. Dispose is to be called by developers to free up the resources as soon as they see it that its no longer needed for them. If they forget to call Dispose then Framework calls the finalize in its own GC cycle (usually will take its own sweet time).
  2. If your object uses disposable objects internally - You implement Dispose() if you created and retained a reference to any object of a type which implements Dispose() and which you haven't already disposed.
  3. If neither of the above is the case (you are NOT dealing with unmanaged resources nor your object uses disposable objects internally) - Then don't do anything. Don't implement Finalize nor Dispose.

Some classic examples:

System.IO.FileStream object manages the lock/stream handles to files. So it implements both dispose and finalize. If the developer disposes it then the other program can access it right away. If he forgets to dispose it then Framework finalize it and close the handles later in its GC cycle.

System.Text.StringBuilder dose not have any unmanaged resource. So no dispose no finalize.

As far as the pattern is concerned what it means to

// Code to dispose the managed resources of the class

is that call the Dispose methods of any .NET objects that you have as components inside that class

And

// Code to dispose the un-managed resources of the class

Means to close the raw handles and pointers. Here is your updated code with examples

class Test : IDisposable
{
  private bool isDisposed = false;

  ~Test()
  {
    Dispose(false);
  }

  protected void Dispose(bool disposing)
  {
    if (!isDisposed)
    {
      if (disposing)
      {
        // Code to dispose the managed resources of the class
        internalComponent1.Dispose();
        internalComponent2.Dispose();
      }

      // Code to dispose the un-managed resources of the class
      CloseHandle(handle);
      handle = IntPtr.Zero;   

      isDisposed = true;
    }
  }

  public void Dispose()
  {
    Dispose(true);
    GC.SuppressFinalize(this);
  }
}

Here is an old question explaining it


If a Foo has resources which will benefit from deterministic cleanup, but none that can be usefully cleaned up in a finalizer, it should implement IDisposable but should not override Finalize or have a destructor. If a class holds multiple resources, and at least one can be cleaned up in a finalizer, then each discrete resource that could be cleaned up in a finalizer should be encapsulated into its own Finalizer/destructor-equipped object (which may be defined in a protected nested class), and the class that would contain those resources should hold references to the wrapper objects. Once that is done, the outer class will fit the pattern for classes with a Dispose method but no finalizer/destructor.