diff -r 6a981ec4d58b -r e98b09018d32 src/linked_list.c --- a/src/linked_list.c Mon Apr 18 14:41:19 2022 +0200 +++ b/src/linked_list.c Mon Apr 18 15:29:52 2022 +0200 @@ -733,7 +733,20 @@ return cx_ll_insert_iter(iter, &elem, prepend); } +static void cx_ll_destructor(CxList *list) { + cx_linked_list *ll = (cx_linked_list *) list; + + cx_linked_list_node *node = ll->begin; + while (node) { + void *next = node->next; + cxFree(list->allocator, node); + node = next; + } + // do not free the list pointer, this is just a destructor! +} + static cx_list_class cx_linked_list_class = { + cx_ll_destructor, cx_ll_add, cx_ll_insert, cx_ll_insert_iter, @@ -747,6 +760,7 @@ }; static cx_list_class cx_pointer_linked_list_class = { + cx_ll_destructor, cx_pll_add, cx_pll_insert, cx_pll_insert_iter, @@ -759,20 +773,6 @@ cx_pll_iterator, }; -static CxList *cx_ll_default_destructor(CxList *list) { - cx_linked_list *ll = (cx_linked_list *) list; - - cx_linked_list_node *node = ll->begin; - while (node) { - void *next = node->next; - cxFree(list->allocator, node); - node = next; - } - - cxFree(list->allocator, list); - return NULL; -} - CxList *cxLinkedListCreate( CxAllocator const *allocator, CxListComparator comparator, @@ -784,7 +784,6 @@ list->base.cl = &cx_linked_list_class; list->base.allocator = allocator; - list->base.list_destructor = (cx_destructor_func) cx_ll_default_destructor; list->base.cmpfunc = comparator; list->base.itemsize = item_size; list->base.capacity = SIZE_MAX; @@ -802,7 +801,6 @@ list->base.cl = &cx_pointer_linked_list_class; list->base.allocator = allocator; - list->base.list_destructor = (cx_destructor_func) cx_ll_default_destructor; list->base.cmpfunc = comparator; list->base.itemsize = sizeof(void *); list->base.capacity = SIZE_MAX; @@ -821,7 +819,9 @@ if (list == NULL) return NULL; cx_for_n (i, num_items) { if (0 != cxListAdd(list, ((const unsigned char *) array) + i * item_size)) { - return cx_ll_default_destructor(list); + cx_ll_destructor(list); + cxFree(allocator, list); + return NULL; } } return list;