Destructor for static fields. Singleton realization

后端 未结 3 1734
醉话见心
醉话见心 2020-12-28 19:42

So, classic simple Singleton realization is following:

class Singleton
{
private:
    static Singleton* singleton;
    Singleton() {}
public:
    static Sing         


        
3条回答
  •  情书的邮戳
    2020-12-28 20:25

    Why you should avoid such code, when there is no matching delete for new

    While there is no actual memory leak (in most modern operating systems), worse thing is that your Singleton destructor doesn't get called. And if you acquire some resources, they propbably would leak.

    What can be done here

    Use smart pointer to store instance, consider std::unique_ptr (with C++11) or boost::auto_ptr

提交回复
热议问题