From 6fa450d7d56cf3535648fb02f92f042df6c928fc Mon Sep 17 00:00:00 2001 From: Gertjan van den Burg Date: Fri, 20 Feb 2015 16:39:52 +0100 Subject: fixed some memory leaks and made prctile actual percentile --- Makefile | 2 +- include/gensvm_train_dataset.h | 6 ++++-- src/gensvm_init.c | 1 + src/gensvm_train_dataset.c | 23 ++++++++++++++++------- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index a128e87..e796cb6 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ VERSION=0.1 CC=gcc -CFLAGS=-Wall -O3 -DVERSION=$(VERSION) +CFLAGS=-Wall -O3 -DVERSION=$(VERSION) -g INCLUDE= -Iinclude LIB= -Llib diff --git a/include/gensvm_train_dataset.h b/include/gensvm_train_dataset.h index 0dc4319..d201aa5 100644 --- a/include/gensvm_train_dataset.h +++ b/include/gensvm_train_dataset.h @@ -18,6 +18,10 @@ #include "globals.h" #include "types.h" +// forward declarations +struct GenData; +struct GenModel; + /** * @brief A structure for a single task in the queue. * @@ -142,6 +146,4 @@ void start_training(struct Queue *q); double gensvm_cross_validation(struct GenModel *model, struct GenData **train_folds, struct GenData **test_folds, int folds, long n_total); - - #endif diff --git a/src/gensvm_init.c b/src/gensvm_init.c index 45373f1..6228d9a 100644 --- a/src/gensvm_init.c +++ b/src/gensvm_init.c @@ -305,6 +305,7 @@ void gensvm_free_data(struct GenData *data) free(data->Z); free(data->RAW); } + free(data->kernelparam); free(data->y); free(data->Sigma); free(data); diff --git a/src/gensvm_train_dataset.c b/src/gensvm_train_dataset.c index eee4bf9..f0c4e93 100644 --- a/src/gensvm_train_dataset.c +++ b/src/gensvm_train_dataset.c @@ -63,12 +63,12 @@ void make_queue(struct Training *training, struct Queue *queue, N *= training->Nc > 0 ? training->Nc : 1; N *= training->Nd > 0 ? training->Nd : 1; - queue->tasks = Malloc(struct Task *, N); + queue->tasks = Calloc(struct Task *, N); queue->N = N; // initialize all tasks for (i=0; iID = i; task->train_data = train_data; task->test_data = test_data; @@ -232,11 +232,14 @@ int doublesort(const void *elem1, const void *elem2) * * @param[in] values array of doubles * @param[in] N length of the array - * @param[in] p percentile to calculate ( 0 <= p <= 1.0 ). + * @param[in] p percentile to calculate ( 0 <= p <= 100.0 ). * @returns the p-th percentile of the values */ double prctile(double *values, long N, double p) { + if (N == 1) + return values[0]; + long i; double pi, pr, boundary; double *local = Malloc(double, N); @@ -244,6 +247,7 @@ double prctile(double *values, long N, double p) local[i] = values[i]; qsort(local, N, sizeof(double), doublesort); + p /= 100.0; p = p*N + 0.5; pi = maximum(minimum(floor(p), N-1), 1); pr = maximum(minimum(p - pi, 1), 0); @@ -383,9 +387,9 @@ void consistency_repeats(struct Queue *q, long repeats, TrainType traintype) p = 0.0; bool breakout = false; while (breakout == false) { - pi = prctile(mean, N, (100.0-p)/100.0); - pr = prctile(std, N, p/100.0); - pt = prctile(time, N, p/100.0); + pi = prctile(mean, N, (100.0-p)); + pr = prctile(std, N, p); + pt = prctile(time, N, p); for (i=0; ikernelparam = NULL; + gensvm_free_model(model); free(nq->tasks); free(nq); - free(model); free(perf); free(std); free(mean); @@ -640,6 +646,8 @@ void start_training(struct Queue *q) note("\nTotal elapsed training time: %8.8f seconds\n", elapsed_time(main_s, main_e)); + // make sure no double free occurs with the copied kernelparam + model->kernelparam = NULL; gensvm_free_model(model); for (f=0; f