Disposing Samples c#

587 views Asked by At

Looking at the sample code of MSDN

// Design pattern for a base class.
public class Base: IDisposable
{
    private bool disposed = false;

    //Implement IDisposable.
    public void Dispose()
    {
          Dispose(true);      <---- 1 Here
          GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
         if (!disposed)
         {
             if (disposing)
             {
                 // Free other state (managed objects).
             }
             // Free your own state (unmanaged objects).
             // Set large fields to null.
             disposed = true;
         }
    }

   // Use C# destructor syntax for finalization code.
   ~Base()
   {
       // Simply call Dispose(false).
       Dispose (false);
   }
}

I don't seem to get the line of code pointed in the first arrow (<----- 1 Here).

I was confused in the first arrow as to Whom the Dispose belong. to the IDisposable or the virtual Dispose of the base?

Any Help that would help me is Great and Very much appreciated!

3

There are 3 answers

0
Jaanus Varus On BEST ANSWER

In that case, the Dispose belongs to the caller aka dev.

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

This is never automatically called, but always by the developer. The true flag indicates that the dispose was explicitly called. GC.SuppressFinalize(this); tells the Garbage Collector that the disposal of that object is already taken care of. No need to call the finalize block.


~Base()
{
    // Simply call Dispose(false).
    Dispose (false);
}

This is automatically called by the Garbage Collector if the object has not been explicitly disposed. It is undetermined when exactly it is called, but sometime when the object has gone out of scope and is marked to be garbage collected. The false flag indicates that this is an automatic disposal in which case managed objects no longer need to be explicitly disposed.

Also, an additional note about finalizers taken from Ben Watson's Writing High-Performance .NET Code

Never implement a finalizer unless it is required. Finalizers are code, triggered by the garbage collector to cleanup unmanaged resources. They are called from a single thread, one after the other, and only after the garbage collector declares the object dead after a collection. This means that if your class implements a finalizer, you are guaranteeing that it will stay in memory even after the collection that should have killed it. This decreases overall GC efficiency and ensures that your program will dedicate CPU resources to cleaning up your object.

6
Marc Gravell On

The purpose of the Dispose(true) / Dispose(false) is so that the real code - the virtual void Dispose(bool) - knows whether it is being called via dispose or finalize. The Dispose(bool) is specific to the class (the virtual method); IDisposable only has a parameterless Dispose() method. As such:

  • the Dispose(true) route will only be called via IDisposable - which typically means via using
  • the Dispose(false) route will only be called via the garbage collector

(note that the Dispose(true) route disables the finalization step once complete)

This boolean flag is important, because during finalization you can't touch any other managed resources (because order is non-deterministic) - only unmanaged. The fact that this method is virtual means that subclasses can add their own cleanup code into the mix.

In reality, though, it is very rare that you need to implement a finalizer; you should not add this much boilerplate routinely.

0
Siamak Ferdos On

This implementation:

public void Dispose(){...}

is for using others from this class's Dispose method. and this implementation:

protected virtual void Dispose(bool disposing){...}

is for using class itself means if others use Dispose() on their code, then can do something else that managed on protected method, but if garbage collector dispose it, it wouldn't run managed code