Skip to content

Commit

Permalink
Fixed memory leak in Format O4
Browse files Browse the repository at this point in the history
- Fixed memory leak for trivial bracket, mentioned in issue #7
  • Loading branch information
benruijl committed Jul 26, 2020
1 parent 60d662a commit 4aee9f8
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions sources/optimize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1650,6 +1650,10 @@ vector<WORD> simulated_annealing() {
vector<WORD> tree = Horner_tree(optimize_expr, state);
int curscore = count_operators_cse_topdown(tree);

// clean poly_vars, that are allocated by Horner_tree
AN.poly_num_vars = 0;
M_free(AN.poly_vars,"poly_vars");

std::vector<WORD> best = state; // best state
int bestscore = curscore;

Expand All @@ -1670,6 +1674,10 @@ vector<WORD> simulated_annealing() {
vector<WORD> tree = Horner_tree(optimize_expr, state);
int newscore = count_operators_cse_topdown(tree);

// clean poly_vars, that are allocated by Horner_tree
AN.poly_num_vars = 0;
M_free(AN.poly_vars,"poly_vars");

if (newscore <= curscore || 2.0 * wranf(BHEAD0) / (float)(UWORD)(-1) < exp((curscore - newscore) / T)) {
curscore = newscore;

Expand Down Expand Up @@ -4615,6 +4623,9 @@ int Optimize (WORD exprnr, int do_print) {
(optimize_expr[optimize_expr[0]]==0 && optimize_expr[0]==ABS(optimize_expr[optimize_expr[0]-1])+1) ||
(optimize_expr[optimize_expr[0]]==0 && optimize_expr[0]==8 &&
optimize_expr[5]==1 && optimize_expr[6]==1 && ABS(optimize_expr[7])==3)) {
if (AO.OptimizeResult.code != NULL)
M_free(AO.OptimizeResult.code, "optimize output");

// zero terms or one trivial term (number or +/-variable), so no
// optimization, so copy expression; special case because without
// operators the optimization crashes
Expand Down

0 comments on commit 4aee9f8

Please sign in to comment.