Commit df6bbb23 authored by peastman's avatar peastman
Browse files

Fixed memory leaks when minimizer fails

parent 6231d53f
...@@ -408,6 +408,7 @@ int lbfgs( ...@@ -408,6 +408,7 @@ int lbfgs(
pf = (lbfgsfloatval_t*)vecalloc(param.past * sizeof(lbfgsfloatval_t)); pf = (lbfgsfloatval_t*)vecalloc(param.past * sizeof(lbfgsfloatval_t));
} }
try {
/* Evaluate the function value and its gradient. */ /* Evaluate the function value and its gradient. */
fx = cd.proc_evaluate(cd.instance, x, g, cd.n, 0); fx = cd.proc_evaluate(cd.instance, x, g, cd.n, 0);
if (0. != param.orthantwise_c) { if (0. != param.orthantwise_c) {
...@@ -613,6 +614,26 @@ int lbfgs( ...@@ -613,6 +614,26 @@ int lbfgs(
*/ */
step = 1.0; step = 1.0;
} }
}
catch (...) {
vecfree(pf);
/* Free memory blocks used by this function. */
if (lm != NULL) {
for (i = 0;i < m;++i) {
vecfree(lm[i].s);
vecfree(lm[i].y);
}
vecfree(lm);
}
vecfree(pg);
vecfree(w);
vecfree(d);
vecfree(gp);
vecfree(g);
vecfree(xp);
throw;
}
lbfgs_exit: lbfgs_exit:
/* Return the final value of the objective function. */ /* Return the final value of the objective function. */
......
...@@ -105,12 +105,13 @@ static lbfgsfloatval_t evaluate(void *instance, const lbfgsfloatval_t *x, lbfgsf ...@@ -105,12 +105,13 @@ static lbfgsfloatval_t evaluate(void *instance, const lbfgsfloatval_t *x, lbfgsf
void LocalEnergyMinimizer::minimize(Context& context, double tolerance, int maxIterations) { void LocalEnergyMinimizer::minimize(Context& context, double tolerance, int maxIterations) {
const System& system = context.getSystem(); const System& system = context.getSystem();
int numParticles = system.getNumParticles(); int numParticles = system.getNumParticles();
lbfgsfloatval_t *x = lbfgs_malloc(numParticles*3);
if (x == NULL)
throw OpenMMException("LocalEnergyMinimizer: Failed to allocate memory");
double constraintTol = context.getIntegrator().getConstraintTolerance(); double constraintTol = context.getIntegrator().getConstraintTolerance();
double workingConstraintTol = std::max(1e-4, constraintTol); double workingConstraintTol = std::max(1e-4, constraintTol);
double k = tolerance/workingConstraintTol; double k = tolerance/workingConstraintTol;
lbfgsfloatval_t *x = lbfgs_malloc(numParticles*3);
if (x == NULL)
throw OpenMMException("LocalEnergyMinimizer: Failed to allocate memory");
try {
// Initialize the minimizer. // Initialize the minimizer.
...@@ -182,6 +183,11 @@ void LocalEnergyMinimizer::minimize(Context& context, double tolerance, int maxI ...@@ -182,6 +183,11 @@ void LocalEnergyMinimizer::minimize(Context& context, double tolerance, int maxI
} }
} }
} }
}
catch (...) {
lbfgs_free(x);
throw;
}
lbfgs_free(x); lbfgs_free(x);
// If necessary, do a final constraint projection to make sure they are satisfied // If necessary, do a final constraint projection to make sure they are satisfied
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment