一种内存泄漏的代码

内存泄漏的地方经常比较隐秘,今天就遇到了一回。问题出在delete (void*)上面,示例代码如下:

class Detect  
{
public:
    Detect()
    {
        m_n1 = 0;
        m_n2 = 0;
        m_data.resize(10 , 0);
    }
    virtual ~Detect()
    {    
    }
    
protected:
    int m_n1;
    int m_n2;
    deque<short> m_data;
};

class Monitor  
{
public:
    Monitor()
    {
        m_pDetect = new Detect;
    }
    ~Monitor()
    {
        if (m_pDetect != 0)
        {
// 加上这行代码就避免泄漏内存 //((Detect
*)m_pDetect)->~Detect(); delete m_pDetect; m_pDetect = 0; } } bool detect() { return true; } protected: void* m_pDetect; };

 

posted @ 2015-10-21 16:25  lichongbin  阅读(247)  评论(0编辑  收藏  举报