Skip to content

Commit

Permalink
use unique_ptr for EWSB solvers
Browse files Browse the repository at this point in the history
in order to not leak ressource if an exception is thrown between
creation and deletion of the solvers.
  • Loading branch information
Alexander Voigt authored and Alexander Voigt committed Aug 6, 2016
1 parent 4e324e3 commit 92ef454
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
5 changes: 4 additions & 1 deletion meta/EWSB.m
Expand Up @@ -663,8 +663,11 @@
Quit[1];
];

MakeUniquePtr[str_String, obj_String] :=
"std::unique_ptr<" <> obj <> ">(" <> str <> ")";

CreateEWSBRootFinders[rootFinders_List] :=
Utils`StringJoinWithSeparator[CreateEWSBRootFinder /@ rootFinders, ",\n"];
Utils`StringJoinWithSeparator[MakeUniquePtr[#,"EWSB_solver"]& /@ (CreateEWSBRootFinder /@ rootFinders), ",\n"];

WrapPhase[phase_ /; phase === Null, str_String] :=
str;
Expand Down
7 changes: 3 additions & 4 deletions templates/mass_eigenstates.cpp.in
Expand Up @@ -49,6 +49,7 @@

#include <cmath>
#include <iostream>
#include <memory>
#include <algorithm>

#ifdef ENABLE_THREADS
Expand Down Expand Up @@ -298,7 +299,7 @@ int CLASSNAME::solve_ewsb_iteratively()
{
EWSB_args params = {this, ewsb_loop_order};

EWSB_solver* solvers[] = {
std::unique_ptr<EWSB_solver> solvers[] = {
@EWSBSolvers@
};

Expand All @@ -316,7 +317,7 @@ int CLASSNAME::solve_ewsb_iteratively()
int status;
for (std::size_t i = 0; i < number_of_solvers; ++i) {
VERBOSE_MSG("\tStarting EWSB iteration using solver " << i);
status = solve_ewsb_iteratively_with(solvers[i], x_init);
status = solve_ewsb_iteratively_with(solvers[i].get(), x_init);
if (status == EWSB_solver::SUCCESS) {
VERBOSE_MSG("\tSolver " << i << " finished successfully!");
break;
Expand All @@ -339,8 +340,6 @@ int CLASSNAME::solve_ewsb_iteratively()
#endif
}

std::for_each(solvers, solvers + number_of_solvers, Delete_object());

return status;
}

Expand Down

0 comments on commit 92ef454

Please sign in to comment.