title: Subclass finalizers
body: <p>I've got a C# project that calls into a 3rd party unmanaged DLL for controlling a PCI card. Their API documentation states that a call to <code>CardClose</code> must be made before my application terminates. Because the card is multifunctional, I've written two interfaces for it and implemented them using separate singleton classes. There is also a base class for sharing common information, such as the <code>CardHandle</code>, and for generating the singleton instances (factory pattern).</p>
  
  <p>My challenge is figuring out how to use finalizers in the subclasses to call <code>CardClose</code> when the class is disposed. But I only want that to happen when both classes have been disposed, otherwise the card becomes unavailable to the other class that is still using it. (I realize you wouldn't normally dispose of a singleton, but I have reasons for doing so in this case.) One solution is to combine these classes into one, but I'm curious if there's a better way.</p>
  
  <p>Here's the dispose code for the base class:</p>
  
  <pre><code>    protected static int CardHandle { get; set; } = -1;
      protected static bool IsAvailable { get; set; } = false;
      protected static uint InstancesCount { get; private set; } = 0;
  
      #region IDisposable Support
      private bool m_alreadyDisposed = false; // To detect redundant calls
  
      protected virtual void Dispose(bool disposing) // Overridden by subclasses which should then call base.Dispose(disposing)
      {
          if (m_alreadyDisposed) return;
  
          if (disposing)
          {
              // Dispose managed state (managed objects). 
          }
  
          // Free unmanaged resources (unmanaged objects) and override a finalizer below.
          InstancesCount--;
          m_alreadyDisposed = true;
      }
  
      // Override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
      //~Arinc()
      //{
      //    // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
      //    Dispose(false);
      //}
  
      public void Dispose()
      {
          // Put cleanup code in Dispose(bool disposing) above.
          Dispose(true); // When overriden, this will call the derived class's Dispose(boolean).
          GC.SuppressFinalize(this);
      }
      #endregion
  </code></pre>
  
  <p>First subclass with finalizer:</p>
  
  <pre><code>    #region IDisposable Support
      private bool m_alreadyDisposed = false; // To detect redundant calls
  
      protected override void Dispose(bool disposing)
      {
          if (m_alreadyDisposed) return;
  
          if (disposing)
          {
              // Dispose managed state (managed objects).
              Arinc429Device = null; // This class instance, stored as a property.
          }
  
          // Free unmanaged resources (unmanaged objects) and override a finalizer below.
          if (InstancesCount == 1) // This is the last instance if true.
          {
              L43_CardReset(CardHandle); // Unmanaged code call.
              L43_CardClose(CardHandle); // Unmanaged code call.
              CardHandle = -1;
              IsAvailable = false;
          }
  
          m_alreadyDisposed = true;
  
          base.Dispose(disposing);
      }
  
      ~Arinc429Ballard()
      {
          // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
          Dispose(false);
      }
      #endregion
  </code></pre>
  
  <p>The second subclass uses the same dispose code but with a different instance property. As you can see, I'm attempting to keep track of the total instances count using the <code>InstancesCount</code> property which is incremented by the factory. When the last instance is disposed, the card is closed, but this approach is proving to be less than reliable.</p>