Skip to content

Commit

Permalink
Merge pull request #47 from Maluuba/plug-training-leaks
Browse files Browse the repository at this point in the history
Plug some memory leaks that occur during training.
  • Loading branch information
Naoaki Okazaki committed Jan 24, 2016
2 parents a349159 + 328f6b3 commit 5554ee4
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions frontend/learn.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ static void learn_option_finish(learn_option_t* opt)
{
int i;

free(opt->logbase);
free(opt->model);
free(opt->algorithm);
free(opt->type);

for (i = 0;i < opt->num_params;++i) {
free(opt->params[i]);
Expand Down
3 changes: 3 additions & 0 deletions frontend/reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ int read_data(FILE *fpi, FILE *fpo, crfsuite_data_t* data, int group)
/* Unrecognized declaration. */
fprintf(fpo, "\n");
fprintf(fpo, "ERROR: unrecognized declaration: %s\n", token->attr);
iwa_delete(iwa);
return -1;
}
} else {
Expand Down Expand Up @@ -148,5 +149,7 @@ int read_data(FILE *fpi, FILE *fpo, crfsuite_data_t* data, int group)
progress(fpo, prev, 100);
fprintf(fpo, "\n");

iwa_delete(iwa);

return n;
}
1 change: 1 addition & 0 deletions lib/cqdb/src/cqdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ int cqdb_writer_close(cqdb_writer_t* dbw)

/* Initialize the file header. */
strncpy((char*)header.chunkid, CHUNKID, 4);
header.flag = 0;
header.byteorder = BYTEORDER_CHECK;
header.bwd_offset = 0;
header.bwd_size = dbw->bwd_num;
Expand Down
17 changes: 17 additions & 0 deletions lib/crf/src/crf1d_encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ static void crf1de_init(crf1de_t *crf1de)

static void crf1de_finish(crf1de_t *crf1de)
{
int i;

if (crf1de->ctx != NULL) {
crf1dc_delete(crf1de->ctx);
crf1de->ctx = NULL;
Expand All @@ -107,10 +109,16 @@ static void crf1de_finish(crf1de_t *crf1de)
crf1de->features = NULL;
}
if (crf1de->attributes != NULL) {
for (i = 0; i < crf1de->num_attributes; ++i) {
free(crf1de->attributes[i].fids);
}
free(crf1de->attributes);
crf1de->attributes = NULL;
}
if (crf1de->forward_trans != NULL) {
for (i = 0; i < crf1de->num_labels; ++i) {
free(crf1de->forward_trans[i].fids);
}
free(crf1de->forward_trans);
crf1de->forward_trans = NULL;
}
Expand Down Expand Up @@ -917,6 +925,14 @@ static int encoder_objective_and_gradients(encoder_t *self, floatval_t *f, float
return 0;
}

static void encoder_release(encoder_t *self)
{
crf1de_t *crf1de = (crf1de_t*)self->internal;
crf1de_finish(crf1de);
free(crf1de);
free(self);
}

encoder_t *crf1d_create_encoder()
{
encoder_t *self = (encoder_t*)calloc(1, sizeof(encoder_t));
Expand All @@ -936,6 +952,7 @@ encoder_t *crf1d_create_encoder()
self->viterbi = encoder_viterbi;
self->partition_factor = encoder_partition_factor;
self->objective_and_gradients = encoder_objective_and_gradients;
self->release = encoder_release;
self->internal = enc;
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/crf/src/crfsuite_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ struct tag_encoder

int (*save_model)(encoder_t *self, const char *filename, const floatval_t *w, logging_t *lg);

void (*release)(encoder_t *self);
};

/**
Expand Down
8 changes: 8 additions & 0 deletions lib/crf/src/crfsuite_train.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,16 @@ static void crfsuite_train_delete(crfsuite_trainer_t* self)
{
crfsuite_train_internal_t *tr = (crfsuite_train_internal_t*)self->internal;
if (tr != NULL) {
if (tr->gm != NULL) {
tr->gm->release(tr->gm);
}
if (tr->params != NULL) {
tr->params->release(tr->params);
}
free(tr->lg);
free(tr);
}
free(self);
}

static int crfsuite_train_addref(crfsuite_trainer_t* tr)
Expand Down Expand Up @@ -207,6 +211,10 @@ static int crfsuite_train_train(
gm->save_model(gm, filename, w, lg);
}

if (0 <= holdout) {
dataset_finish(&testset);
}
dataset_finish(&trainset);
free(w);

return 0;
Expand Down
2 changes: 2 additions & 0 deletions lib/crf/src/params.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ static int params_release(crfsuite_params_t* params)
free(pars->params[i].val_s);
free(pars->params[i].help);
}
free(pars->params);
free(pars);
free(params);
}
return count;
}
Expand Down
1 change: 1 addition & 0 deletions lib/crf/src/rumavl.c
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,7 @@ static RUMAVL_NODE *node_new(RUMAVL *tree, const void *record)
*--------------------------------------------------------------------------*/
static void node_destroy (RUMAVL *tree, RUMAVL_NODE *node)
{
mem_free(tree, node->rec);
mem_free(tree, node);
}

Expand Down

0 comments on commit 5554ee4

Please sign in to comment.