【发布时间】:2010-12-22 01:33:28
【问题描述】:
考虑下面的代码。
using boost::shared_ptr;
struct B;
struct A{
~A() { std::cout << "~A" << std::endl; }
shared_ptr<B> b;
};
struct B {
~B() { std::cout << "~B" << std::endl; }
shared_ptr<A> a;
};
int main() {
shared_ptr<A> a (new A);
shared_ptr<B> b (new B);
a->b = b;
b->a = a;
return 0;
}
没有输出。 没有调用析构函数。内存泄漏。 我一直认为智能指针有助于避免内存泄漏。
如果我需要在类中进行交叉引用,我应该怎么做?
【问题讨论】:
标签: c++ boost memory-leaks shared-ptr smart-pointers