fix memory leak in test data

Sat, 16 Apr 2022 21:12:02 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 16 Apr 2022 21:12:02 +0200
changeset 520
f937c6d11d1f
parent 519
79d14e821b3a
child 521
e5dc54131d55

fix memory leak in test data

test/test_list.cpp file | annotate | diff | comparison | revisions
--- a/test/test_list.cpp	Sat Apr 16 20:44:47 2022 +0200
+++ b/test/test_list.cpp	Sat Apr 16 21:12:02 2022 +0200
@@ -57,20 +57,26 @@
 const ptrdiff_t loc_data = offsetof(struct node, data);
 
 struct node_test_data {
-    node *begin;
+    node *begin = nullptr;
 
-    explicit node_test_data(node *begin) : begin(begin) {}
-    node_test_data(node_test_data&) = delete;
-    node_test_data(node_test_data&&) = default;
+    explicit node_test_data(node *begin) : begin(begin) {
+        auto n = begin;
+        while (n != nullptr) {
+            nodes.push_back(n);
+            n = n->next;
+        }
+    }
+
+    node_test_data(node_test_data &) = delete;
+
+    node_test_data(node_test_data &&) = default;
 
     ~node_test_data() {
-        auto n = begin;
-        while (n != nullptr) {
-            auto next = n->next;
-            delete n;
-            n = next;
-        }
+        for (auto &&n: nodes) delete n;
     }
+
+private:
+    std::vector<node *> nodes;
 };
 
 static node_test_data create_nodes_test_data(size_t len) {

mercurial