Skip to content

Commit

Permalink
Merge pull request #166 from nbelakovski/fix_x0_and_possible_memory_leak
Browse files Browse the repository at this point in the history
Fix x0 and possible memory leak
  • Loading branch information
zaikunzhang committed Feb 22, 2024
2 parents 6253280 + f0f125b commit b625d64
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions c/prima.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ int prima_init_result(prima_result_t *result, prima_problem_t *problem)
if (!result->x)
return PRIMA_MEMORY_ALLOCATION_FAILS;
for (int i = 0; i < problem->n; i++)
result->x[i] = NAN;
result->x[i] = problem->x0[i];

// f: objective function value at the returned point
result->f = NAN;
Expand All @@ -119,8 +119,10 @@ int prima_init_result(prima_result_t *result, prima_problem_t *problem)
result->nlconstr = NULL;
else {
result->nlconstr = (double*)malloc(problem->m_nlcon * sizeof(double));
if (!result->nlconstr)
if (!result->nlconstr) {
free(result->x);
return PRIMA_MEMORY_ALLOCATION_FAILS;
}
for (int i = 0; i < problem->m_nlcon; i++)
result->nlconstr[i] = NAN;
}
Expand Down Expand Up @@ -244,10 +246,10 @@ int prima_minimize(const prima_algorithm_t algorithm, prima_problem_t *problem,
{
int use_constr = (algorithm == PRIMA_COBYLA);

int info = prima_init_result(result, problem);
int info = prima_check_problem(problem, options, use_constr, algorithm);

if (info == 0)
info = prima_check_problem(problem, options, use_constr, algorithm);
info = prima_init_result(result, problem);

if (info == 0) {
switch (algorithm) {
Expand Down

0 comments on commit b625d64

Please sign in to comment.