From df2129a1bb24d73777bb0ffc8d52daf4b7271f2d Mon Sep 17 00:00:00 2001 From: FReenen Date: Sun, 14 Apr 2024 12:28:27 +0200 Subject: [PATCH 1/3] history: fix memory leak --- CLI/CLI.c | 2 +- CMDList/CMDList.c | 2 +- History/History.c | 6 +++++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CLI/CLI.c b/CLI/CLI.c index 3daced7..0f09a60 100644 --- a/CLI/CLI.c +++ b/CLI/CLI.c @@ -68,7 +68,7 @@ bool CLI_deinit() FIFOBuffChar_delete(FIFO); #ifdef HISTORY - History_deinit(History); + History_deinit(History, true); #endif return true; } diff --git a/CMDList/CMDList.c b/CMDList/CMDList.c index d785fd3..d3233e5 100644 --- a/CMDList/CMDList.c +++ b/CMDList/CMDList.c @@ -44,7 +44,7 @@ int CMDList_add(CMDList_t *list, CMD_t* cmd, char* cmdName) read_p = cmd->cmd; } - while (returnCode == 0) + while (returnCode >= 0) { char c = *read_p & (~0x20); // convert to uppercase if ((c >= 'A') && (c <= 'Z')) diff --git a/History/History.c b/History/History.c index 74bdca0..10d60d9 100644 --- a/History/History.c +++ b/History/History.c @@ -19,7 +19,7 @@ History_t* History_init() } -int History_deinit(History_t* history) +int History_deinit(History_t* history, bool freeLines) { // Check if the buffer there is something in the buffer if (history->FirstEl_p == NULL) @@ -29,6 +29,10 @@ int History_deinit(History_t* history) while (el != NULL) { History_element_t* nextEl = el->nextEl_p; + if (freeLines) + { + free(el->line); + } free(el); el = nextEl; } From 62f45f725623cb0b1e7a851c96ee94a8a71bc16c Mon Sep 17 00:00:00 2001 From: FReenen Date: Sun, 14 Apr 2024 22:47:18 +0200 Subject: [PATCH 2/3] fix memory leaks --- CLI/CLI.c | 1 + CMDList/CMDList.c | 5 +++-- History/history.h | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/CLI/CLI.c b/CLI/CLI.c index 0f09a60..66644ca 100644 --- a/CLI/CLI.c +++ b/CLI/CLI.c @@ -6,6 +6,7 @@ #include // #include +#define HISTORY #include "../FIFOBuff/FIFOBuffChar.h" #ifdef HISTORY diff --git a/CMDList/CMDList.c b/CMDList/CMDList.c index d3233e5..a29db52 100644 --- a/CMDList/CMDList.c +++ b/CMDList/CMDList.c @@ -17,7 +17,8 @@ CMDList_t* CMDList_init() // does not free up the commands int CMDList_deinit(CMDList_t *list) { - CMDList_t** list_p = (CMDList_t**)list; + CMDList_t** list_p; + *list_p = list; int i; for (i = 0; i < 26; i++) @@ -28,7 +29,7 @@ int CMDList_deinit(CMDList_t *list) } } - free(*list_p); + free(list); return 0; } diff --git a/History/history.h b/History/history.h index 35bc020..04e2a8d 100644 --- a/History/history.h +++ b/History/history.h @@ -22,7 +22,7 @@ typedef struct History_s { extern History_t* History_init(); // destroy a fifo buffer (free up its space) -extern int History_deinit(History_t* history); +extern int History_deinit(History_t* history, bool freeLines); // put value i in buffer if there is still memory avaliable extern int History_put(History_t *history, char* line); From 3ed2ab432badce39765cac7338843eaf619d4d4e Mon Sep 17 00:00:00 2001 From: FReenen Date: Sun, 14 Apr 2024 23:30:44 +0200 Subject: [PATCH 3/3] update --- CMDList/CMDList.c | 7 +++---- CMakeLists.txt | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/CMDList/CMDList.c b/CMDList/CMDList.c index a29db52..d24b445 100644 --- a/CMDList/CMDList.c +++ b/CMDList/CMDList.c @@ -8,17 +8,16 @@ // initilises a CMDList_t with all NULL pointers CMDList_t* CMDList_init() { - CMDList_t *list = malloc(sizeof(CMDList_t)); + CMDList_t* list = malloc(sizeof(CMDList_t)); memset(list, 0, sizeof(CMDList_t)); return list; } // free up the full tree from memory // does not free up the commands -int CMDList_deinit(CMDList_t *list) +int CMDList_deinit(CMDList_t* list) { - CMDList_t** list_p; - *list_p = list; + CMDList_t** list_p = (CMDList_t**)list; int i; for (i = 0; i < 26; i++) diff --git a/CMakeLists.txt b/CMakeLists.txt index ec5cf73..0905f99 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ add_library(history History/History.c) # CLI add_library(CLI CLI/CLI.c) -target_link_libraries(CLI CMDList FIFOBuffChar) +target_link_libraries(CLI CMDList FIFOBuffChar history) add_library(CLI_History CLI/CLI.c) target_link_libraries(CLI_History CMDList FIFOBuffChar history)