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?
Constructing an anonymous temporary shared pointer, for instance inside the arguments to a function call:
f(shared_ptr<Foo>(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.