Is a singleton link going to stack or heap?

I read a lot of articles about singles, but no one touches on my problem. I understand that Singletons should be used only when necessary, and in my game I use them for certain parts of the engine.

However, I initially had my singletones as pointers like this:

static MapReader* Instance()
{
    if (instance == 0)
    {
        instance = new MapReader();
        return instance;
    }
    return instance;
}

However, it always seemed to me that using too many pointers is bad for leaks, and I prefer not to use them if I can help (or smart pointers if I need to). So I changed all my singletones to these links:

static MapReader& Instance()
{
    static MapReader instance;
    return instance;
}

However, now I notice that my game lags behind at odd times and then speeds up like FPS is a little awkward.

, ; ? ? ?

+4
2

, new.

static, . . , C++11 3.7.1:

1/ , , , . (3.6.2, 3.6.3).

2/ , , , , / , 12.8.

3/ static .

4/ , , .

, .

, , .

, . , , , .


, . -, , . , Instance().

, A if, B , B , , . A , .

, .

- . if, , :

static MapReader* Instance() {
    if (instance == 0)
        instance = new MapReader();
    return instance;
}
+3

. . .

+1

All Articles