[C++-sig] Re: understanding pointer ownership with Boost.Python

David Abrahams dave at boost-consulting.com
Wed Nov 24 18:02:40 CET 2004


"Alexis H. Rivera-Rios" <ahrivera at yahoo.com> writes:

> You mentioned the AddStrategy is not exception safe. 
> Can you explain me why or point me to a resource that
> explains the problem

Well, *probably* not.  It depends on how you've documented it.

Before it is called, the caller of AddStrategy has responsibility for
deleting the pointer.  After it is called, the target object has
responsibility for deleting the pointer.  Who has responsibility if
the push_back throws an exception?

When answering consider this code:

  x.AddStrategy(new Foo);

where Foo is an appropriate typedef.

> and possible solution alternatives?

Use std::auto_ptr or boost::shared_ptr to manage ownership instead of
passing raw pointers.

HTH,

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com




More information about the Cplusplus-sig mailing list