Singleton Destructors

后端 未结 12 1942
一整个雨季
一整个雨季 2021-01-31 03:55

Should Singleton objects that don\'t use instance/reference counters be considered memory leaks in C++?

Without a counter that calls for explicit deletion of the singlet

12条回答
  •  佛祖请我去吃肉
    2021-01-31 04:36

    Each language and environment will differ, though I agree with @Aaron Fisher that a singleton tends to exist for the duration of the process.

    In the example of C++, using a typical singleton idiom:

    Singleton &get_singleton()
    {
       static Singleton singleton;
       return singleton;
    }
    

    the Singleton instance will be constructed the first time the function is called, and the same instance will have it's destructor called during the global static destructor phase at program shutdown.

提交回复
热议问题