Skip to content

Commit

Permalink
Merge pull request chokkan#34 from timdawborn/fix-liblbfgs_lbfgs-x-al…
Browse files Browse the repository at this point in the history
…location

Corrected necessary call to lbfgs_malloc before lbfgs is called.
(@timdawborn is absolutely right!)
  • Loading branch information
Naoaki Okazaki committed Jan 24, 2016
2 parents 6abdc70 + 9972969 commit 07a66ee
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lib/crf/src/train_lbfgs.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,9 @@ int crfsuite_train_lbfgs(
memset(&opt, 0, sizeof(opt));
lbfgs_parameter_init(&lbfgsparam);

/* Allocate an array that stores the current weights. */
w = (floatval_t*)calloc(sizeof(floatval_t), K);
/* Allocate an array that stores the current weights. As per the liblbfgs
* documentation, this needs to be allocated with lbfgs_malloc. */
w = lbfgs_malloc(K);
if (w == NULL) {
ret = CRFSUITEERR_OUTOFMEMORY;
goto error_exit;
Expand Down Expand Up @@ -318,21 +319,21 @@ int crfsuite_train_lbfgs(
logging(lg, "L-BFGS terminated with error code (%d)\n", lbret);
}

/* Restore the feature weights of the last call of lbfgs_progress(). */
veccopy(w, lbfgsi.best_w, K);
/* Set the best_w array (allocated by us) as the result array, which the
* callee can safely `free`. */
*ptr_w = lbfgsi.best_w;

/* Report the run-time for the training. */
logging(lg, "Total seconds required for training: %.3f\n", (clock() - begin) / (double)CLOCKS_PER_SEC);
logging(lg, "\n");

/* Exit with success. */
free(lbfgsi.best_w);
*ptr_w = w;
/* Exit with success. */
lbfgs_free(w);
return 0;

error_exit:
free(lbfgsi.best_w);
free(w);
lbfgs_free(w);
*ptr_w = NULL;
return ret;
}

0 comments on commit 07a66ee

Please sign in to comment.