如何删除Singleton指针? [英] How to delete Singleton pointer?

查看:186
本文介绍了如何删除Singleton指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个单例模式。这是,我在GetInstance中创建一个Singleton *的新实例,当我尝试和删除它在析构函数,它在无限循环。在这种情况下如何避免内存泄漏?



请参考下面的代码:

  #define NULL 0 
class Singleton
{
private:
static Singleton * m_pInstance;
Singleton(){};

public:

static Singleton * GetInstance()
{
if(m_pInstance == NULL)
{
m_pInstance = new Singleton();
}
return m_pInstance;
}

〜Singleton()
{
//删除m_pInstance; //如果我取消注释
m_pInstance = NULL,系统进入infine循环;
}
};

Singleton * Singleton :: m_pInstance = NULL;

int main()
{
Singleton * pInstance = Singleton :: GetInstance();
delete pInstance;
}


解决方案

循环!



你调用析构函数,但析构函数也调用析构函数,所以析构函数会再次调用析构函数... ...



如果你想使用 delete ,你必须从析构函数 之外使用它,并且在析构函数中不再调用它。 / p>

要做到这一点,你可以使用另一个静态方法来镜像 GetInstance()方法:

  class Singleton 
{
public:

...

//此方法是GetInstance的镜像
static void ResetInstance()
{
delete m_pInstance; // REM:即使指针为NULL(也不做任何操作),它仍然有效
m_pInstance = NULL; //所以GetInstance仍然可以工作。
}

...

〜Singleton()
{
// do destructor stuff:free allocated ressources if any。
...
}

注意:其他人警告您使用一个单身,他们是正确的,因为这种模式经常被滥用。所以在使用它之前。但是,无论如何,这是学习的好方法!


I was implementing a singleton pattern.Here,I am creating a new instance of Singleton* in GetInstance, when I try and delete it in the destructor, it does in infinite loop. How to avoid memory leak in this case ?

Please refer the below piece of code:

#define NULL 0
class Singleton  
{ 
    private :  
        static Singleton* m_pInstance;  
        Singleton(){};  

    public :

    static Singleton* GetInstance()
    {
        if(m_pInstance == NULL)
        {
            m_pInstance  = new Singleton();         
        }
        return m_pInstance;
    }

    ~Singleton()
    { 
        //delete m_pInstance; // The system goes in infinate loop here if i uncomment this  
        m_pInstance = NULL;
    }
};

Singleton*  Singleton ::m_pInstance = NULL;   

int main()  
{
    Singleton* pInstance = Singleton::GetInstance();
    delete pInstance;  
}     

解决方案

Of course it causes an infinite loop !

You call the destructor, but the destructor also calls the destructor, so the destructor calls the destructor again... and again...

If you want to use delete, you must use it from outside of the destructor and NOT call it again in the destructor.

To do that, you can use another static method which will mirror the GetInstance() method :

class Singleton  
{ 
public :

   ...

   // this method is a mirror of GetInstance
   static void ResetInstance()
   {
      delete m_pInstance; // REM : it works even if the pointer is NULL (does nothing then)
      m_pInstance = NULL; // so GetInstance will still work.
   }

   ...

   ~Singleton()
   { 
       // do destructor stuff : free allocated ressources if any.
       ...
   }

Note : the other people warn you about using a singleton and they are right because this pattern is often misused. So think before using it. But go ahead anyway, that is the good way to learn !

这篇关于如何删除Singleton指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆