This is a mirror of official site: http://jasper-net.blogspot.com/

IDisposable, Finalizer, and SuppressFinalize in C# and C++/CLI

| Monday, January 30, 2012
The .NET Framework features an interface called IDisposable. It basically exists to allow freeing unmanaged resources (think: C++ pointers). In most cases, you won’t need IDisposable when writing C# code. There are some exceptions though, and it becomes more important when writing C++/CLI code.

The help page for IDisposable provides the code for IDisposable's default implementation pattern in C#. This article will explain each part of it step by step and also provide the equivalent C++/CLI code in each step.
Summary – for the impatient

Here’s the summary of this article for those who don’t want to read the actual explanations.

Rules:

    For a class owning managed resources, implement IDisposable (but not a finalizer).
    For a class owning at least one unmanaged resource, implement both IDisposable and a finalizer.

C# code:

class DataContainer : IDisposable {
  public void Dispose() {
    Dipose(true);
    GC.SuppressFinalizer(this);
  }

  ~DataContainer() { // finalizer
    Dispose(false);
  }

  protected virtual void Dispose(bool disposing) {
    if (m_isDisposed)
      return;

    if (disposing) {
      // Dispose managed data
      //m_managedData.Dispose();
    }
    // Free unmanaged data
    //DataProvider.DeleteUnmanagedData(m_unmanagedData);
    m_isDisposed = true;
  }

  private bool m_disposed = false;
}

C++/CLI code:

ref class DataContainer {
public:
  ~DataContainer() {
    if (m_isDisposed)
       return;

    // dispose managed data
    //delete m_managedData;
    this->!DataContainer(); // call finalizer
    m_isDisposed = true;
  }

  // Finalizer
  !DataContainer() {
    // free unmanaged data
    //DataProvider::DeleteUnmanagedData(m_unmanagedData);
  }

private:
  bool m_isDisposed; // must be set to false
};

The Root of all Evil

In C#, all classes are managed by the garbage collector. However, some things just can’t be expressed in pure managed code. In these cases you’ll need to store unmanaged data in a managed class. Examples are file handles, sockets, or objects created by unmanaged functions/frameworks.

Read more: Codeproject
QR: IDisposable-Finalizer-and-SuppressFinalize-in-Csha

Posted via email from Jasper-net

0 comments: