From 607d2104343e515fbe682c39ed46875516e7f301 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Mon, 9 Oct 2023 08:49:00 -0500 Subject: [PATCH] fix memory leak --- assignments/01/lc_openmp.c | 5 +++++ assignments/01/lc_pthreads.c | 11 +++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/assignments/01/lc_openmp.c b/assignments/01/lc_openmp.c index 85e8c80..10185cf 100644 --- a/assignments/01/lc_openmp.c +++ b/assignments/01/lc_openmp.c @@ -136,6 +136,11 @@ int main(int argc, char **argv) { printf("num correct: %d, out of %d (%.2f%%)\n", num_correct, test_data->rows, (100.0 * num_correct) / test_data->rows); + + free(test_data->buf); + free(test_label->buf); + free(test_data); + free(test_label); } free(w); diff --git a/assignments/01/lc_pthreads.c b/assignments/01/lc_pthreads.c index be8f31e..05a04f7 100644 --- a/assignments/01/lc_pthreads.c +++ b/assignments/01/lc_pthreads.c @@ -35,6 +35,8 @@ int main(int argc, char **argv) { if (data->dimensions < thread_count) thread_count = data->dimensions; + pthread_t *thread_pool = malloc(thread_count * sizeof(pthread_t)); + int *wtf = malloc(thread_count * sizeof(int)); w = calloc(data->dimensions, sizeof(FLOAT)); new_w = calloc(data->dimensions, sizeof(FLOAT)); @@ -49,8 +51,6 @@ int main(int argc, char **argv) { double iter_start_time = monotonic_seconds(); // Spawn N threads - pthread_t *thread_pool = malloc(thread_count * sizeof(pthread_t)); - int *wtf = malloc(thread_count * sizeof(int)); for (int t = 0; t < thread_count; ++t) { wtf[t] = t; pthread_create(&thread_pool[t], NULL, each_thread, &wtf[t]); @@ -99,6 +99,8 @@ int main(int argc, char **argv) { free(labels->buf); free(data); free(labels); + free(thread_pool); + free(wtf); // NOTE: NOT PART OF THE ASSIGNMENT // Perform validation to see how well the model performs on training data @@ -123,6 +125,11 @@ int main(int argc, char **argv) { printf("num correct: %d, out of %d (%.2f%%)\n", num_correct, test_data->rows, (100.0 * num_correct) / test_data->rows); + + free(test_data->buf); + free(test_label->buf); + free(test_data); + free(test_label); } free(w);