C ++ SmartPointers泄漏自我分配?

时间:2009-12-03 14:47:15

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

我有一个小问题,理解为什么我的智能指针类正在泄漏自我分配。 如果我做这样的事情

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;
    }
};

6 个答案:

答案 0 :(得分:3)

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

应该是:

    if ( this != & other ) 

答案 1 :(得分:2)

您可能希望查看A Proposal to Add General Purpose Smart Pointers to the Library Technical Report中的以下引文:

  

Boost开发人员发现共享所有权智能指针非常难以正确实现。其他人也做了同样的观察。例如,Scott Meyers [Meyers01]说:

     
    

“STL本身不包含引用计数智能指针,并且编写一个好的指针 - 一直可以正常工作 - 非常棘手,除非必须,否则你不想这样做。我发布了代码对于1996年更有效的C ++中的引用计数智能指针,尽管它基于已建立的智能指针实现并将其提交给经验丰富的开发人员进行广泛的预发布审查,但有效的错误报告已经流行了多年。参考计数智能指针可能失败的微妙方式的数量是显着的。“

  

如果这是作业,请阅读有关如何使用swap()(成员)函数实现复制ctor和赋值运算符的内容。否则,不要尝试编写自己的智能指针。 You cannot win

答案 2 :(得分:1)

我也没有看到泄漏,但我认为还有一些其他问题(除了许多编译器错误 - 这不是你正在使用的代码):

SmartPtr& operator=(SmartPtr& other)

应该通过const引用获取参数。您不必增加其他的引用计数,因为您可以在非const左侧进行,因为它们将共享相同的引用计数实例。

接下来,为这些类实现赋值的规范方法是使用copy-and-swap idiom - 这意味着你还应该定义一个简单的交换方法(它只是交换指针),而不用担心自我赋值:)

答案 3 :(得分:0)

我的印象是没有内存泄漏。 可以肯定:

  • 使用valgrind或VS-alternative进行测试
  • 使用std :: tr1 :: shared_ptr(如果这不仅仅是教育用途)

答案 4 :(得分:0)

您的代码无法编译,这让我相信您发布的版本不是您同事抱怨的版本。

答案 5 :(得分:-1)

几乎任何智能指针都会出现漏洞的情况。如果使用引用实现它,它就是它必须的方式。还有一百万个其他问题加上它们很慢。因为它们比原始指针更笨拙,所以如果你所有的东西都是引用计数,那真的没什么用。我一直试图将它们用于某些非常特殊的目的,但它们不适用于一般的编程用途。例如,有一个原因是它们不允许在STL容器中使用。

相关问题