相关疑难解决方法(0)

使用boost :: shared_ptr时有什么潜在的危险?

使用时,你有什么方法可以用脚射击自己boost::shared_ptr?换句话说,当我使用时,我必须避免哪些陷阱boost::shared_ptr

c++ boost pointers shared-ptr

49
推荐指数
6
解决办法
1万
查看次数

如何使用shared_ptr避免内存泄漏?

请考虑以下代码.

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;
}
Run Code Online (Sandbox Code Playgroud)

没有输出.没有调用desctructor.内存泄漏.我一直认为智能指针有助于避免内存泄漏.

如果我需要在类中进行交叉引用,我该怎么办?

c++ boost memory-leaks smart-pointers shared-ptr

23
推荐指数
1
解决办法
2万
查看次数

标签 统计

boost ×2

c++ ×2

shared-ptr ×2

memory-leaks ×1

pointers ×1

smart-pointers ×1