Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
tsoc
openmm
Commits
df6bbb23
Commit
df6bbb23
authored
Feb 10, 2017
by
peastman
Browse files
Fixed memory leaks when minimizer fails
parent
6231d53f
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
262 additions
and
235 deletions
+262
-235
libraries/lbfgs/src/lbfgs.cpp
libraries/lbfgs/src/lbfgs.cpp
+192
-171
openmmapi/src/LocalEnergyMinimizer.cpp
openmmapi/src/LocalEnergyMinimizer.cpp
+70
-64
No files found.
libraries/lbfgs/src/lbfgs.cpp
View file @
df6bbb23
...
@@ -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. */
...
...
openmmapi/src/LocalEnergyMinimizer.cpp
View file @
df6bbb23
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment