From 18c7f70fca5cee82b55ea2595a6af6b673aa7e12 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Tue, 20 May 2025 07:07:04 -0500 Subject: [PATCH] refactor(doubly linked list): changed method used for freeing memory on error --- Src/Core/DoublyLinkedList.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/Src/Core/DoublyLinkedList.c b/Src/Core/DoublyLinkedList.c index f7e07ee..5669073 100644 --- a/Src/Core/DoublyLinkedList.c +++ b/Src/Core/DoublyLinkedList.c @@ -26,7 +26,7 @@ #include #include #include - +#include FledastyError fledasty_doubly_linked_list_initialize(FledastyDoublyLinkedList *new_doubly_linked_list, void *values, const size_t values_size, const size_t element_byte_size) { if (new_doubly_linked_list == NULL) { return FLEDASTY_ERROR_FAILED_ALLOCATION; @@ -55,20 +55,22 @@ FledastyError fledasty_doubly_linked_list_initialize(FledastyDoublyLinkedList *n new_doubly_linked_list->start->previous = NULL; new_doubly_linked_list->start->next = NULL; + FledastyDoublyLinkedListNode *last_node = new_doubly_linked_list->start; for (size_t index = new_doubly_linked_list->element_byte_size; index < new_doubly_linked_list->size * new_doubly_linked_list->element_byte_size; index += new_doubly_linked_list->element_byte_size) { FledastyDoublyLinkedListNode *new_node = (FledastyDoublyLinkedListNode*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode)); if (new_node == NULL) { - FledastyDoublyLinkedListNode *previous_node = NULL; FledastyDoublyLinkedListNode *current_node = new_doubly_linked_list->start; - while (current_node != NULL) { - previous_node = current_node; + while (current_node->next != NULL) { current_node = current_node->next; - hallocy_free(previous_node->value); - hallocy_free(previous_node); + hallocy_free(current_node->previous->value); + hallocy_free(current_node->previous); } + hallocy_free(current_node->value); + hallocy_free(current_node); + return FLEDASTY_ERROR_FAILED_ALLOCATION; } @@ -76,16 +78,17 @@ FledastyError fledasty_doubly_linked_list_initialize(FledastyDoublyLinkedList *n if (new_node->value == NULL) { hallocy_free(new_node); - FledastyDoublyLinkedListNode *previous_node = NULL; FledastyDoublyLinkedListNode *current_node = new_doubly_linked_list->start; - while (current_node != NULL) { - previous_node = current_node; + while (current_node->next != NULL) { current_node = current_node->next; - hallocy_free(previous_node->value); - hallocy_free(previous_node); + hallocy_free(current_node->previous->value); + hallocy_free(current_node); } + hallocy_free(current_node->value); + hallocy_free(current_node); + return FLEDASTY_ERROR_FAILED_ALLOCATION; }