标签归档:引用计数

RSS feed of 引用计数

最后更新于 .

C/C++代码中,野指针问题历来已久,当然,大家都知道new/delete要成对出现:

A *p = new A();
delete p;
p = NULL;

然而现实中却并不是总是如此简单,考虑如下例子:

class A
{
public:
    C() {}
    virtual ~C() {}
};
class B
{
public:
    B() {
        m_pA = NULL;
    }
    virtual ~B() {}

    void SetA(A* p)
    {
        m_pA = p;
    }

private:
    A* m_pA;
};

A* pA = new A();
B* pB = new B();
pB->SetA(pA);

delete pA;
pA = NULL;
//此时B中的m_pA已经无效 ...