16

This is what I understand about IDisposable and finalizers from "CLR via C#", "Effective C#" and other resources:

  • IDisposable is for cleaning up managed and unmanaged resources deterministically.
  • Classes that are responsible for unmanaged resources (e.g. file handles) should implement IDisposable and provide a finalizer to guarantee that they are cleaned up even if the client code does not call Dispose() on the instance.
  • Classes that are responsible for managed resources only should never implement a finalizer.
  • If you have a finalizer then you must implement IDisposable (this allows client code to do the right thing and call Dispose(), while the finalizer prevents leaking resources if they forget).

While I understand the reasoning for and agree with all of the above, there is one scenario where I think it makes sense to break these rules: a singleton class that is responsible for unmanaged resources (such as providing a single point of access to particular files).

I believe it is always wrong to have a Dispose() method on a singleton because the singleton instance should live for the life of the application and if any client code calls Dispose() then you are stuffed. However, you want a finalizer so that when the application is unloaded the finalizer can clean up the unmanaged resources.

So having a singleton class with a finalizer that does not implement IDisposable seems to me to be a reasonable thing to do, yet this type of design is counter to what I understand are best practices.

Is this a reasonable approach? If not, why not and what are the superior alternatives?

6 Answers 6

6

I'd first mention that Object Oriented design patterns and their consequences do not always influence every language decision, even in Object Oriented languages. You can certainly find classic design patterns that are easier to implement in one language (Smalltalk) as opposed to another (C++).

That being said, I'm not sure I agree with the premise that a singleton instance should only be disposed at the end of an application. Nothing in the design pattern descriptions that I've read for Singleton (or Design Patterns: Elements of reusable Object-Oriented Software) mention this as a property of this pattern. A singleton should ensure that only one instance of the class exists at any one moment in time; that does not imply that it must exist for as long as the application exists.

I have a feeling that in practice, many singletons do exist for most of the life of an application. However, consider an application that uses a TCP connection to communicate with a server, but can also exist in a disconnected mode. When connected, you would want a singleton to maintain the connection information and state of the connection. Once disconnected, you may want to keep that same singleton - or you may dispose of the singleton. While some may argue that it makes more sense to keep the singleton (and I may even be among them), there is nothing in the design pattern itself that precludes you from disposing of it - if a connection is remade, the singleton can be instantiated again, as no instance of it exists at that moment in time.

In other words, you can create scenarios where it is logical for singletons to have IDisposable.

3
  • 2
    Fact is, if you can conceive of a reason for your object to be replaced during the app's lifetime, the object can not legitimately be a singleton. As for why...Say i grabbed the Singleton and passed it to stuff that needed it (cause, ya know, dependency injection is a good thing). Then something somewhere decides to replace it. Now there are two singletons (the old one i have been passing around, and the new one). This very possibility violates the entire definition of Singleton -- that is, that code always sees exactly one instance.
    – cHao
    Jan 30, 2012 at 22:23
  • So, first of all, holy old answer Batman. Second, your example doesn't seem to make sense. If you pass an instance of a Singleton to an object and then some other object decides it wants the Singleton, then the pattern allows for that - it should get a reference back to the object that is currently in memory. The point I was making was that the pattern doesn't preclude you from disposing of the object. In that case, using your example, if the object was disposed then the single instance should no longer exist, so when a different object requests the Singleton, it should get a new instance. Feb 9, 2012 at 23:34
  • 4
    If the object is disposed, the instance does still exist -- anyone who'd acquired it before it was disposed would still have it. So it obviously can't be GCed, but it's now unusable. Think about the consequences of that statement in a multithreaded context. An expression like Singleton.getInstance().doStuff() is no longer thread safe, even if both getInstance and doStuff are. A thread could come along in between the two calls and dispose the instance we just acquired. And that's even assuming your mythical everybody-always-calls-getInstance scenario.
    – cHao
    Feb 10, 2012 at 4:40
5

If the unmanaged resource is released only on application exit you don't even need to worry with a finalizer since the process unload should deal with this for you anyway.

If you have multiple app domains and you want to deal with an app domain unload that's one possible issue but possibly one you don't need to care about.

I second those saying that this design is possibly not the right thing to do (and will make it harder to fix is subsequently you find that you do actually need two instances) Create the object (or a lazy loading wrapper object) in your entry point and pass it through the code to where it is needed making it clear who is responsible for providing it to whom, then you are free to change the decision to use only one with little effect to the rest of the code (which uses what it gets given)

4

As long as your finalizer doesn't call methods (such as Dispose) on any other managed objects, you should be fine. Just remember that finalization order is not deterministic. That is, if your singleton object Foo holds a reference to object Bar that requires disposal, you cannot reliably write:

~Foo()
{
    Bar.Dispose();
}

The garbage collector might have collected Bar already.

At the risk of stepping into a pile of OO goo (i.e. starting a war), one alternative to using a singleton is to use a static class.

7
  • +1 for static class - singletons are not as necessary as their ubiquity would suggest. Jan 21, 2009 at 1:11
  • Conceptually the only difference between a singleton and a static class is that a singleton is one static field that points at many instance fields, where a static class is a bunch of static fields. When it comes to resource management, the differences between the 2 are negligible. Jan 21, 2009 at 6:10
  • what scott said - disposable singletons are just a fundamental problem
    – annakata
    Jan 21, 2009 at 13:01
  • 1
    Can the GC really have collected Bar? From what I've read elsewhere, while Bar may have been finalized, objects which are referenced by one that's awaiting finalization Foo are eligible for finalization but not garbage-collection; I am seeking some clarification on that point, though. Of course, calling Bar.Dispose will be unsafe unless one knows that it will not acquire any locks, create any new objects, or throw any exceptions (I think even a caught exception may be dangerous due to the creation of the Exception object), but other actions might be safe.
    – supercat
    Jul 26, 2010 at 17:52
  • @supercat: The order of finalization is not guaranteed. If an object is in the finalization queue, then its references to other objects don't exist for the purposes of determining those objects' eligibility for collection. So, whereas Foo holds a reference to Bar, it's similar to a weak reference. So Bar can be collected before Foo is finalized. Jan 24, 2012 at 18:16
2

While it may get you code review gripes and FxCop warnings, there's nothing intrinsically wrong with implementing a finalizer without IDisposable. However, doing so on a singleton is not a reliable way to capture process or AppDomain tear-down.

At the risk of offering subjective design advice: If the object is truly stateless, make it a static class. If it is stateful, question why it is a Singleton: you're creating a mutable global variable. If you're trying to capture application close, deal with it when your main loop exits.

2
  • "If the object is truly stateless, make it a static class. If it is stateful, question why it is a Singleton" I completely agree, but I like the snappiness of the quoted. Got a reference for that?
    – annakata
    Jan 21, 2009 at 13:05
  • I made that one up on the fly. I've been railing against most uses of Singletons for some time now on the Portland Pattern Repository due to their effect on causality in the code. Jan 22, 2009 at 3:35
0

Applicability of Singleton to any particular situation aside,

I think there is nothing wrong with Disposing of Singleton. In combination with lazy instantiation it just means that you release resource if you do not need it temporarily, and then re-acquire it as needed.

1
  • That one piece of code might acquire and "Dispose" a singleton says nothing about whether anyone else might still be using it. If the finalizer gets called, nobody's using it; note that the only way that would likely happen would be if the static/global reference to the singleton were a WeakReference. Otherwise that reference would itself protect the singleton from finalization.
    – supercat
    Jul 26, 2010 at 17:44
0

If you want to create a singleton with a finalizer, you should probably have the static reference to it be a WeakReference. This will require a little bit of extra work to ensure thread-safety in the accessor, but it will allow the singleton to be garbage-collected when nobody is using it (if someone subsequently calls the GetInstance() method, they'll get a new instance). If a static strong reference were used, it would keep the singleton instance alive even if there were no other references to it.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.