1

I want to de-allocate the memory from the original singleton object and create a new one with another method.

public sealed class ObjectZ {
    static readonly ObjectZ _instance = new ObjectZ();
    private ObjectZ() {}
    public static ObjectZ Instance{
        get { return _instance; }
    }
}

What would this method look like?

leppie
  • 115,091
  • 17
  • 196
  • 297
Glimpse
  • 462
  • 2
  • 8
  • 25
  • 1
    May I ask you for the reasoning behind this idea? As you've probably noticed, Singleton is about accessing a private static `readonly` instance of some class to prevent exactly the behavior you're describing (and to make sure there's only one instance at a time of course). – walther Apr 02 '13 at 23:02
  • ^ Exactly, not to mention your proposal wouldn't work with your current code given you're using a readonly field, you'd be better off creating a static accessor class that caches a private instance of the "singleton" and a method to invalidate the cached item and create a new one. – Clint Apr 02 '13 at 23:05
  • 3
    The best moment in your debugging life would be when there are 2 singletons alive at the same time... Here are some useful letters - @$%#%#$%# - to use when you find out that someone correctly cached "singleton" instance and than you recreate it :) – Alexei Levenkov Apr 02 '13 at 23:15
  • @AlexeiLevenkov ... especially when the app is multi-threaded B^) – Nicholas Carey Apr 02 '13 at 23:51
  • My only goal is to access this object instance from any other class without having to pass it in as a parameter for a constructor or method. Am I at least accomplishing that with the code above? I just need the same instance every time I retrieve it, until I'm done with it. Then I recycle. – Glimpse Apr 03 '13 at 15:15

1 Answers1

2

Singletons are usually created once and exist for the lifetime of the domain, recreating a singleton is dodgy business and by definition the code I've provided isn't truly a singleton.

The behaviour you seem to be after is a statically accessible single object cache that can be invalidated.

public static class SingletonAccessor
{
    private static SomeClass _instance;
    private static object _lock = new Object();

    public static SomeClass Singleton
    {
        get
        {
            lock (_lock)
            {
                if (_instance == null)
                {
                    _instance = new SomeClass();
                }

                return _instance;
            }
        }
    }

    public static void Recycle()
    {
        lock (_lock)
        {
            if (_instance != null)
            {
                // Do any cleanup, perhaps call .Dispose if it's needed

                _instance = null;
            }
        }
    }
}
Clint
  • 6,133
  • 2
  • 27
  • 48
  • lock (_lock) throws an error if _lock remains null. Perhaps it's better to change `private static object _lock;` to `private static object _lock = new Object();` like shown here https://msdn.microsoft.com/en-us/library/c5kehkcz.aspx – H B Mar 03 '15 at 07:56
  • @HB probably, though some degree of knowledge and common sense is assumed on the part of the person using code snippets they find on the internet. – Clint Mar 03 '15 at 15:10