0

Does the following psuedo code accomplish my goal of cleaning up after myself when my DLL is being hosted by code I don't control?

  • More specifically, how do I clean up my objects created in my static constructor?

  • Do I need to suppress Finalize in the Disposable?

  • Am I guaranteed that the compiler or something will call IDisposable even if the Host doesn't?

Psuedo code:

public class MyDLL  : SomeHostICantControl, IDisposable
{
    public SpinLock MyDictionarySpinlock = new Spinlock; // approximate syntax
    public static Dictionary<string, string> MyDictionary = null;
    public static Timer MyCleanUpTimer = null;

    public static MyDLL()
    {
        // Set up Cache Here ... how do I dispose of it?
        MyDictionary = new Dictionary<string, string>();

       // remove old data from the dictionary, use spinlock to sync writes
       // or use concurrent dictionary in .NET 4
       MyCleanUpTimer = new Timer(); // run every hour
    }

    // many instances will be created and disposed of.  I have no control when or how often
    public void MyDll()
    {
       this.MyHostEvent += New Event Handler....
    }

    //.. some event handler code here


    public void Dispose()
    {
       this.MyHostEvent -= Event Handler.;
       // Do I need to suppressFinalize here?
    }

    //~MyDll()
   //{
   // Is this the right place to clean up after my constuctor?
   //}
}
makerofthings7
  • 60,103
  • 53
  • 215
  • 448

1 Answers1

1

Answering your questions in order:

Static fields exist for the lifetime of the application, as such they are "cleaned up" as a result of the application exiting (memory is reclaimed, files are closed etc.). You don't appear to be doing anything that might require explicit action to be taken to clean-up (e.g. flushing buffered data in a StreamWriter to file), but perhaps there are details missing in your code snippet.

You need to suppress finalize in your Dispose() method if your class has a finalizer (yours appears not to as it's commented out), or if someone can derive from your class and may introduce unmanaged resources that may need cleaning up. The latter applies here as your class is not marked sealed, so you should suppress finalize in Dispose().

The runtime will not call Dispose(), however it will call the finalizer if one is present. Note however that as you class instances don't appear to be using any unmanaged resources, a finalizer should not be required.

Iridium
  • 23,323
  • 6
  • 52
  • 74
  • So if my DLL host won't call Dispose, and if the CLR doesn't either, does that mean the events my constructor will never be cleaned up unless I put the clean up code in the deconstructor. Just so I'm clear the "~" is the finalizer? I don't want to create a memory leak – makerofthings7 Jun 22 '12 at 14:18
  • Yes, the ~MyDll() is the finalizer, however, remember that there are no longer any references to your class, it will be cleaned up by the GC, and as the event subscription is not static, it will disappear too, without needing to be explicitly unsubscribed. – Iridium Jun 23 '12 at 10:48
  • Thank you, since my Timer is static should I clean up there? If I guess I don't *have* to, and the GC will do it, I guess the finalizer is only for unmanaged code.. – makerofthings7 Jun 23 '12 at 19:19
  • As the timer is static, it will last until the application exits, and when that happens, the timer will be automatically cleaned up. There is nothing that you need to (or can) do. – Iridium Jun 24 '12 at 09:01