Skip to content

Commit

Permalink
use shared_ptr to avoid resource leak if exception is thrown
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Voigt authored and Alexander Voigt committed Aug 8, 2016
1 parent 991eb71 commit 19bb58f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 12 deletions.
13 changes: 3 additions & 10 deletions src/two_scale_solver.cpp
Expand Up @@ -58,7 +58,6 @@ RGFlow<Two_scale>::RGFlow()

RGFlow<Two_scale>::~RGFlow()
{
delete_sliders();
}

/**
Expand All @@ -71,7 +70,7 @@ void RGFlow<Two_scale>::add(Constraint<Two_scale>* c, Two_scale_model* m)
{
if (!c) throw SetupError("constraint pointer is NULL");
if (!m) throw SetupError("model pointer is NULL");
sliders.push_back(new Constraint_slider(m, c));
sliders.push_back(std::make_shared<Constraint_slider>(m, c));
}

/**
Expand All @@ -89,7 +88,7 @@ void RGFlow<Two_scale>::add_upwards(Matching<Two_scale>* mc, Two_scale_model* m1
if (!m1) throw SetupError("model pointer 1 is NULL");
if (!m2) throw SetupError("model pointer 2 is NULL");
mc->set_models(m1, m2);
sliders.push_back(new Matching_up_slider(m1, m2, mc));
sliders.push_back(std::make_shared<Matching_up_slider>(m1, m2, mc));
}

/**
Expand All @@ -107,7 +106,7 @@ void RGFlow<Two_scale>::add_downwards(Matching<Two_scale>* mc, Two_scale_model*
if (!m1) throw SetupError("model pointer 1 is NULL");
if (!m2) throw SetupError("model pointer 2 is NULL");
mc->set_models(m2, m1);
sliders.push_back(new Matching_down_slider(m1, m2, mc));
sliders.push_back(std::make_shared<Matching_down_slider>(m1, m2, mc));
}

/**
Expand Down Expand Up @@ -165,11 +164,6 @@ void RGFlow<Two_scale>::clear_problems()
s->clear_problems();
}

void RGFlow<Two_scale>::delete_sliders()
{
for_each(sliders.begin(), sliders.end(), Delete_object());
}

/**
* Does the initial guess by calling the guess() method of the initial
* guesser (if given).
Expand Down Expand Up @@ -273,7 +267,6 @@ unsigned int RGFlow<Two_scale>::get_max_iterations() const
*/
void RGFlow<Two_scale>::reset()
{
delete_sliders();
sliders.clear();

iteration = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/two_scale_solver.hpp
Expand Up @@ -21,6 +21,7 @@

#include "rg_flow.hpp"

#include <memory>
#include <vector>
#include <string>

Expand Down Expand Up @@ -130,7 +131,7 @@ class RGFlow<Two_scale> {
Matching<Two_scale>* matching;
};

std::vector<Slider*> sliders; ///< sliders to be run up and down
std::vector<std::shared_ptr<Slider> > sliders; ///< sliders to be run up and down
unsigned int iteration; ///< iteration number (starting at 0)
Convergence_tester<Two_scale>* convergence_tester; ///< the convergence tester
Initial_guesser<Two_scale>* initial_guesser; ///< does initial guess
Expand All @@ -142,7 +143,6 @@ class RGFlow<Two_scale> {
bool accuracy_goal_reached() const; ///< check if accuracy goal is reached
void check_setup() const; ///< check the setup
void clear_problems(); ///< clear model problems
void delete_sliders(); ///< delete all sliders
unsigned int get_max_iterations() const; ///< returns max. number of iterations
void initial_guess(); ///< initial guess
double get_precision(); ///< returns running precision
Expand Down

0 comments on commit 19bb58f

Please sign in to comment.