From f0f125bf7e5531d6d3636eef6e5289229b4af37e Mon Sep 17 00:00:00 2001 From: Nickolai Belakovski Date: Wed, 21 Feb 2024 23:10:06 -0800 Subject: [PATCH] Fix x0 and possible memory leak --- c/prima.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/c/prima.c b/c/prima.c index 614a209475..de6919ca5e 100644 --- a/c/prima.c +++ b/c/prima.c @@ -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; @@ -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; } @@ -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) {