Removing C # samples

Looking at the MSDN code example

// 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 can't seem to see the line of code shown in the first arrow (<----- 1 here).

I was confused about the first arrow regarding who Dispose belongs to. to IDisposable or Virtual Recycle Base?

Any help that would help me would be great and greatly appreciated!

+3


source to share


3 answers


In this case, Dispose is owned by the caller aka dev.

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

      

This is never called automatically, but always by the developer. The flag true

indicates that the call to dispose was explicitly called. GC.SuppressFinalize(this);

informs the garbage collector that disposal of this object has already been taken care of. You don't need to call the finalize block.




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

      

This is automatically called by the garbage collector unless the object has been explicitly disposed of. It is undefined when it is exactly called, but sometime when the object is out of scope and is marked as garbage collected. The flag false

indicates that this is automatic deletion, in which case the managed objects no longer need to be explicitly allocated.

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

Never execute a finalizer unless required. Finalizers are code called by the garbage collector to clean up unmanaged resources. They are called from one thread, one after the other, and only after the object is declared dead after being collected by the garbage collector. This means that if your class implements a finalizer, you ensure that it stays in memory even after a collection that should have killed it. This reduces overall GC efficiency and ensures that your program allocates CPU resources to clean up your object.

+2


source


The purpose of Dispose(true)

/ Dispose(false)

is that the real code - virtual void Dispose(bool)

- knows if it will be called via dispose or finalize. Dispose(bool)

belongs to a class (virtual method); IDisposable

only has a method with no parameters Dispose()

. Thus:

  • the route Dispose(true)

    will only be called through IDisposable

    - which usually meansusing

  • the route Dispose(false)

    will only be called through the garbage collector

(note that the route Dispose(true)

disables the completion phase upon completion)



This boolean flag is important because during finalization you cannot touch other managed resources (because the order is not deterministic) - only unmanaged. The fact that this method is equal virtual

means that subclasses can add their own cleanup code to the mix.

In reality, however, very rarely that you need to implement a finalizer; you don't have to add this template regularly.

+4


source


This implementation:

public void Dispose(){...}

      

is intended to be used by other methods of the Dispose class. and this implementation:

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

      

intended to use the class itself, means that others use Dispose()

in their code and then can do something else, which is managed in a protected method, but if the garbage collector removes it, it won't run the managed code

0


source







All Articles