C++ SmartPointers 在 self 分配时泄漏?

标签 c++ pointers memory-leaks smart-pointers

我不太理解为什么我的智能指针类在 self 评估时会泄漏。 如果我做这样的事情

SmartPtr sp1(new CSine());//CSine is a class that implements IFunction iterface
sp1=sp1;

我的同事告诉我,我的智能指针泄漏了。我在我的智能指针中添加了一些日志消息来跟踪正在发生的事情和测试并报告了这一点:

SmartPtr sp1(new CSine());
->CSine constructor
->RefCounter increment 0->1
->RefCounter constructor
->SmartPtr constructor

sp1=sp1;
->checks if this.RefCounter == to parameter.RefCounter, if true returns the smart pointer unmodified else modifies the object and returns it with the new values; in this case it returns true and returns the object unchanged.

at the end
->SmartPtr destructor
->RefCounter decrement 1->0
->RefCounter destructor
->CSine destructor

我不明白为什么他们认为我的智能指针泄漏...有什么想法吗? 提前致谢!

class SmartPtr
{
private:
    RefCounter* refCnt;
    void Clear()
    {
        if(!isNull() && refCnt->Decr() == 0)
            delete refCnt;
        refCnt = 0;
    };
public:
    explicit SmartPtr();
    explicit SmartPtr(IFunction *pt):refCnt(new RefCounter(pt)){};
    SmartPtr(SmartPtr& other)
    {
        refCnt = other.refCnt;
        if (!isNull())
            refCnt->Incr();
    };
    virtual ~SmartPtr(void){Clear();};

    SmartPtr& operator=(SmartPtr& other)
    {
        if(other.refCnt != refCnt)
        {
            if(!rVar.isNull())
                other.refCnt->Incr();
            Clear();
            refCnt = other.refCnt;
        }
        return *this;
    };

    SmartPtr& operator=(IFunction* _p)
    {

        if(!isNull())
        {
            Clear();
        }
        refCnt = new RefCounter(fct);
        return *this;
    };

    IFunction* operator->();
    const IFunction* operator->() const;
    IFunction& operator*();
    const IFunction& operator*() const;
    bool isNull() const { return refCnt == 0; };

    inline bool operator==(const int _number) const;
    inline bool operator!=(const int _number) const;
    inline bool operator==(IFunction* _other) const;
    inline bool operator!=(IFunction* _other) const;
    inline bool operator==(SmartPtr& _other) const;
    inline bool operator!=(SmartPtr& _other) const;
};

class RefCounter
{
    friend class SmartPtr;
private:
    IFunction* p;
    unsigned c;

    explicit RefCounter(IFunction* _p):c(0),p(_p)
    {
        if(_p != NULL)
            Incr();
        cout<<"RefCounter constructor."<<endl;
    }
    virtual ~RefCounter(void)
    { 
        cout<<"RefCounter destructor."<<endl;
        if(c == 0)
            delete p; 
    }
    unsigned  Incr()
    {
        ++c;
        cout<<"RefCounter increment count:"<<c-1<<" to "<<c<<endl;
        return c; 
    }
    unsigned  Decr()
    {
        if(c!=0)
        {
            --c;
            cout<<"RefCounter decrement count:"<<c+1<<" to "<<c<<endl;
            return c;
        }
        else
            return 0;
    }
};

最佳答案

SmartPtr& operator=(SmartPtr& other)
    {
        if(rVar.refCnt != refCnt)

应该是:

    if ( this != & other ) 

关于C++ SmartPointers 在 self 分配时泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1840343/

相关文章:

c - 优化:访问不同内存区域的指针

ios - iOS:确定虚拟内存分配的来源

c++ - 在同一个套接字上同时调用 Boost C++ ASIO 函数 close() 和 async_write() 有什么问题吗?

c++ - 序列化 map 获取额外的空元素

c++ - C/C++ int[] vs int*(指针 vs. 数组表示法)。有什么区别?

c - 给指针赋值时遇到问题

java - 我如何找到导致内存泄漏的应用程序

c++ - 直接丢失或间接丢失-Valgrind问题

c++ - 在 C++ 中将临时转换为引用

c++ - 在 C++ 中删除一个 tbb 线程