Re: How to make this exception-safe
On 19 Nov, 21:26, JoshuaMaur...@gmail.com wrote:
On Nov 18, 10:43 am, Triple-DES <DenPlettf...@gmail.com> wrote:
#include <memory>
V::V() {
v_.reserve(3);
std::auto_ptr<C> c1( new C(2) );
std::auto_ptr<C> c2( new C(1) );
std::auto_ptr<C> c3( new C(3) );
v_.push_back( c1.release() );
v_.push_back( c2.release() );
v_.push_back( c3.release() );
}
This is overly verbose. You could do it just with
#include <memory>
V::V() {
v_.reserve(3);
v_.push_back( new C(2) );
v_.push_back( new C(1) );
v_.push_back( new C(3) );
}
The auto_ptrs in the quoted example do nothing to make your code more
exception safe. It does not address the basic problem.
You have snipped some important context, namely that the constructor
of C may throw. So in this case, if the third constructor call throws,
the other two C objects are leaked. The auto_ptr's were supposed to
fix that.
The basic
problem is for:
vec.push_back(new foo());
and
vec.push_back(some_auto_ptr.release());
the "new foo()" or "release()" may complete successfully, but then
push_back will fail with an exception, and then the pointer will be
lost and we will have a leak. The reserve call fixes this by
guaranteeing that the next push_backs will not fail or throw.
In such situations where I find myself storing dynamically allocated
objects by pointer into vectors, I write code like:
vec.push_back(0);
vec.back() = new foo();
If the vector is on the stack, I have previously defined ON_BLOCK_EXIT
or some sort of stack object whose destructor will free the contents,
so no leaks here. If the vector is a class member, then the class
destructor will delete the contents, so no leaks in that case either.
It works.
It would, if the destructor was ever called. It will not be in the
case of a unhandled exception in the ctor.
I would hazard a guess that the more elegant way is to use a class
template like std::vector which has ownership of the pointers like
std::auto_ptr.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]