-1

Read the question carefully, please.

I was using this base class for my types where I need the Singleton pattern:

#pragma once

template<typename T>
class singleton
{
private:
    static T* g_pInstance;

public:
    static T* getInstance()         { return g_pInstance; }

public:
    singleton()                     { g_pInstance = (T*)this; }
    ~singleton()                    { if(g_pInstance == this) g_pInstance = nullptr; }
};

template<typename T>
T* singleton<T>::g_pInstance = nullptr;

Usage (no *cpp file):

class Any : public singleton<Any> { /* Done */ }

However, now I have a strange situation using such class from static library, g_pInstance pointer is already set to 0xccccccc (not initialized with zero), before anything was fine.

What is the reason?

UPDATE: compiler: vs 2013 x86

Croll
  • 3,631
  • 6
  • 30
  • 63
  • 1
    Google doesn't think singletons are a good idea. Why do you? https://news.ycombinator.com/item?id=2743894 – duffymo Jul 16 '15 at 02:23
  • related: http://stackoverflow.com/questions/9370749/c-singleton-class-inheritance-good-practice – NathanOliver Jul 16 '15 at 02:26
  • 1
    The basic mistake is to use singleton. – Cheers and hth. - Alf Jul 16 '15 at 02:29
  • 9
    BTW your singleton is broken. Since you have a public constructor you can create as many instances of the singleton as you want. Once the last singleton created goes out of scope or it has its destructor called you will set the pointer to `nullptr` and kill all of the other instances of the singleton out there. You should read [C++ Singleton design pattern](http://stackoverflow.com/questions/1008019/c-singleton-design-pattern) – NathanOliver Jul 16 '15 at 02:30
  • @NathanOliver This behavior is what i need and prefer. I need this method only to get the last instance. – Croll Jul 16 '15 at 02:33
  • @duffmo, singletons are great for me since i use OOP but i don't like static or extern. – Croll Jul 16 '15 at 02:33
  • 7
    *"I need this method only to get the last instance"* - Singletons should only ever have one instance. That's what makes them singletons. – Galik Jul 16 '15 at 02:52
  • @Galik you're being offtopic, check my question again and notice that iam asking about causes of that static template value may be not initialized with zero and how to fix it. It is pretty cool that *singletons should return an instance always* but i just don't need it because i have only one instance of class at all time. I really proud of those who noticed that side of my singleton but it is not the question. – Croll Jul 16 '15 at 02:55
  • 1
    *"singletons are great for me since i use OOP"* What a dangerous thought - that OOP would justify the use of singletons. – Drew Dormann Jul 16 '15 at 04:04
  • 5
    you're getting 0xcccccccc because you're [reading uninitialized memory](http://stackoverflow.com/q/370195/995714) – phuclv Jul 16 '15 at 04:09
  • @duffymo in Java yes, lol – Croll Jul 17 '15 at 12:40
  • Design patterns transcend language. Bad idea in every language, not just Java. – duffymo Jul 17 '15 at 12:53
  • related; http://stackoverflow.com/a/4924318/3747990 – Niall Jul 21 '15 at 12:03
  • @Joker You can't truly have a singleton and expose a public default constructor. At the very least, it should be protected. Also, your destructor must be virtual for this to have any chance of success. – Coder Jul 22 '15 at 12:28

4 Answers4

4

Check this out for a simple singleton implementation Singleton instance declared as static variable of GetInstance method

That eliminates your instance pointer initialization issue. Agree with Luu, VC has value of 0xcccccccc assigned to uninitialized pointers. Singleton is bad, as criticized by many, but I still can't completely avoid it :-(

To try answer why the instance pointer is not nullptr when getInstance is invoked. I assume there are different compilation units in the application. If the posted code is in compilation unit A, and the instance pointer exists in static scope of this A compilation unit. I also assume there exist a compilation unit B that have a class Any object declared in static scope of compilation unit B. Since C++ runtime guarantees initialization order of only within same compilation unit, but not across multiple compilation units. The initialization of class Any object in compilation unit B could happen before the initialization of the instance pointer in compilation unit A. That will cause the observation of 0xcccccccc in the instance pointer when getInstance is invoked.

Community
  • 1
  • 1
simon
  • 391
  • 1
  • 9
  • Beware this method will not work with compilers that don't support thread safe static variables (which was the norm not long ago). – Coder Jul 22 '15 at 12:32
3

MSVC has some issue with initializing template statics. It's not something specific to your case.
I believe this could be a solution:

template<> Any* singleton<Any>::g_pInstance = nullptr;

Your singleton is not exactly thread safe, but I guess you know that already:-)

BitWhistler
  • 1,439
  • 8
  • 12
1

implement a singleton without exposing it to a class, so you dont need to initialize it in source. And make private constructor and copy constructor. Im not sure why you need that casting stuff. You can do like this:

#pragma once

template<typename T>
class singleton
{
private:
    singleton();

    singleton(const singleton&);

public:
    static T& getInstance()
    {
         static T instance;
         return instance;
    }
};
Radon
  • 93
  • 1
  • 9
0

I agree with those that complain about your "singleton" class - it enables multiple instances, so it's not a singleton in the standard sense of the term. But if I understand your question correctly, what's actually bothering you is that Any::getInstance() is not initialized by the nullptr assignment, right?

If so, that's an issue I fail to reproduce with g++ (GCC) 4.8.3, clang version 3.4.2, or on ideone. I recommend trying to use your precise code on different compilers to see if you can reproduce the error there. If you can't, it might be a compiler issue: since from what I know, the initialization line you used is indeed the proper way to initialize a static template data member.

Oak
  • 26,231
  • 8
  • 93
  • 152
  • Well i has no multi pointer, vector list array etc, it only has safety check to prevent invalid pointer in case of incorrect usage. I will check tomorrow and reply. – Croll Jul 20 '15 at 23:17