Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Singleton design pattern

Recently I've bumped into a realization/implementation of the Singleton design pattern for C++. It has looked like this (I have adopted it from the real-life example):

// a lot of methods are omitted here class Singleton {    public:        static Singleton* getInstance( );        ~Singleton( );    private:        Singleton( );        static Singleton* instance; }; 

From this declaration, I can deduce that the instance field is initiated on the heap. That means there is a memory allocation. What is completely unclear for me is when exactly the memory is going to be deallocated? Or is there a bug and memory leak? It seems like there is a problem with the implementation.

My main question is, how do I implement it in the right way?

like image 769
Artem Barger Avatar asked Jun 17 '09 16:06

Artem Barger


People also ask

What is singleton pattern in C?

Singleton in C++ Singleton is a creational design pattern, which ensures that only one object of its kind exists and provides a single point of access to it for any other code. Singleton has almost the same pros and cons as global variables. Although they're super-handy, they break the modularity of your code.

What is Singleton design pattern?

In software engineering, the singleton pattern is a software design pattern that restricts the instantiation of a class to one "single" instance. This is useful when exactly one object is needed to coordinate actions across the system.

When should we use Singleton design pattern?

A singleton should be used when managing access to a resource which is shared by the entire application, and it would be destructive to potentially have multiple instances of the same class. Making sure that access to shared resources thread safe is one very good example of where this kind of pattern can be vital.


1 Answers

In 2008 I provided a C++98 implementation of the Singleton design pattern that is lazy-evaluated, guaranteed-destruction, not-technically-thread-safe:
Can any one provide me a sample of Singleton in c++?

Here is an updated C++11 implementation of the Singleton design pattern that is lazy-evaluated, correctly-destroyed, and thread-safe.

class S {     public:         static S& getInstance()         {             static S    instance; // Guaranteed to be destroyed.                                   // Instantiated on first use.             return instance;         }     private:         S() {}                    // Constructor? (the {} brackets) are needed here.          // C++ 03         // ========         // Don't forget to declare these two. You want to make sure they         // are inaccessible(especially from outside), otherwise, you may accidentally get copies of         // your singleton appearing.         S(S const&);              // Don't Implement         void operator=(S const&); // Don't implement          // C++ 11         // =======         // We can use the better technique of deleting the methods         // we don't want.     public:         S(S const&)               = delete;         void operator=(S const&)  = delete;          // Note: Scott Meyers mentions in his Effective Modern         //       C++ book, that deleted functions should generally         //       be public as it results in better error messages         //       due to the compilers behavior to check accessibility         //       before deleted status }; 

See this article about when to use a singleton: (not often)
Singleton: How should it be used

See this two article about initialization order and how to cope:
Static variables initialisation order
Finding C++ static initialization order problems

See this article describing lifetimes:
What is the lifetime of a static variable in a C++ function?

See this article that discusses some threading implications to singletons:
Singleton instance declared as static variable of GetInstance method, is it thread-safe?

See this article that explains why double checked locking will not work on C++:
What are all the common undefined behaviours that a C++ programmer should know about?
Dr Dobbs: C++ and The Perils of Double-Checked Locking: Part I

like image 149
Martin York Avatar answered Oct 01 '22 08:10

Martin York