What are potential dangers when using boost::shared_ptr?

后端 未结 13 747
猫巷女王i
猫巷女王i 2020-12-02 07:42

What are some ways you can shoot yourself in the foot when using boost::shared_ptr? In other words, what pitfalls do I have to avoid when I use boost::shared_ptr?

13条回答
  •  有刺的猬
    2020-12-02 08:45

    Constructing an anonymous temporary shared pointer, for instance inside the arguments to a function call:

    f(shared_ptr(new Foo()), g());
    

    This is because it is permissible for the new Foo() to be executed, then g() called, and g() to throw an exception, without the shared_ptr ever being set up, so the shared_ptr does not have a chance to clean up the Foo object.

提交回复
热议问题