Hi
> I do not have any experience with exception safety in c++. Keir is the
> expert there, but I would like to understand what kind of error are you
> trying to avoid with the use of smart pointers here and what kind of
> exception safety are you looking for?
I was thinking of the following:
problem.AddResidualBlock(
new MyCostFunction(cost_params),
new MyLossFunction(loss_params),
getParameters());
If either getParameters or one of the constructors involved here throws an
exception, there is a potential memory leak (if the cost function or the loss
function object has already been created). You can work around this, but it
can quickly get tedious.
Now with a properly designed smart pointer, the code would look like:
smart_ptr<CostFunction> cost_function(new MyCostFunction(cost_params));
smart_ptr<LossFunction> loss_function(new MyLossFunction(cost_params));
problem.AddResidualBlock(cost_function, loss_function, getParameters());
Here, in case of an exception, all memory will be properly freed when
unwinding the stack.
At the moment, the user must take care that no exceptions are thrown, which
may or may not be possible. Exceptions might be raised by e.g. logging
facilities, parameter checks, other libraries or even by new itself
(bad_alloc). Admittedly this will happen only in exceptional (no pun intended)
circumstances, but the necessary changes are relatively small (I think).
Markus