fix inconsistent destructor requirements for list and map classes

Sun, 21 May 2023 14:56:10 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 21 May 2023 14:56:10 +0200
changeset 708
1caed6c9ba68
parent 707
87eb4bdb2d0e
child 709
1e8ba59e7911

fix inconsistent destructor requirements for list and map classes

src/array_list.c file | annotate | diff | comparison | revisions
src/cx/list.h file | annotate | diff | comparison | revisions
src/linked_list.c file | annotate | diff | comparison | revisions
src/list.c file | annotate | diff | comparison | revisions
--- a/src/array_list.c	Sun May 21 14:40:05 2023 +0200
+++ b/src/array_list.c	Sun May 21 14:56:10 2023 +0200
@@ -169,7 +169,24 @@
 
 static void cx_arl_destructor(struct cx_list_s *list) {
     cx_array_list *arl = (cx_array_list *) list;
+
+    char *ptr = arl->data;
+
+    if (list->simple_destructor) {
+        for (size_t i = 0; i < list->size; i++) {
+            cx_invoke_simple_destructor(list, ptr);
+            ptr += list->item_size;
+        }
+    }
+    if (list->advanced_destructor) {
+        for (size_t i = 0; i < list->size; i++) {
+            cx_invoke_advanced_destructor(list, ptr);
+            ptr += list->item_size;
+        }
+    }
+
     cxFree(list->allocator, arl->data);
+    cxFree(list->allocator, list);
 }
 
 static size_t cx_arl_insert_array(
--- a/src/cx/list.h	Sun May 21 14:40:05 2023 +0200
+++ b/src/cx/list.h	Sun May 21 14:56:10 2023 +0200
@@ -70,6 +70,9 @@
 struct cx_list_class_s {
     /**
      * Destructor function.
+     *
+     * Implementations SHALL invoke the content destructor functions if provided
+     * and SHALL deallocate the list memory, if an allocator is provided.
      */
     void (*destructor)(struct cx_list_s *list);
 
--- a/src/linked_list.c	Sun May 21 14:40:05 2023 +0200
+++ b/src/linked_list.c	Sun May 21 14:56:10 2023 +0200
@@ -876,11 +876,13 @@
 
     cx_linked_list_node *node = ll->begin;
     while (node) {
+        cx_invoke_destructor(list, node->payload);
         void *next = node->next;
         cxFree(list->allocator, node);
         node = next;
     }
-    // do not free the list pointer, this is just a destructor!
+
+    cxFree(list->allocator, list);
 }
 
 static cx_list_class cx_linked_list_class = {
--- a/src/list.c	Sun May 21 14:40:05 2023 +0200
+++ b/src/list.c	Sun May 21 14:56:10 2023 +0200
@@ -273,25 +273,7 @@
 // </editor-fold>
 
 void cxListDestroy(CxList *list) {
-    if (list->simple_destructor) {
-        CxIterator iter = cxListIterator(list);
-        cx_foreach(void*, elem, iter) {
-            // already correctly resolved pointer - immediately invoke dtor
-            list->simple_destructor(elem);
-        }
-    }
-    if (list->advanced_destructor) {
-        CxIterator iter = cxListIterator(list);
-        cx_foreach(void*, elem, iter) {
-            // already correctly resolved pointer - immediately invoke dtor
-            list->advanced_destructor(list->destructor_data, elem);
-        }
-    }
-
     list->cl->destructor(list);
-    if (list->allocator) {
-        cxFree(list->allocator, list);
-    }
 }
 
 int cxListCompare(

mercurial