summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2011-01-10 16:21:24 +1030
committerRusty Russell <rusty@rustcorp.com.au>2011-01-10 16:21:24 +1030
commit8d20b53ecd76773591e95db3bdfcbbd5129efb9e (patch)
treef57010c18f8f5702d3bab8c37baf239fdbad0e7f
parent6a13cea6a9055092f52dec6f74c09d99d673dd0e (diff)
rbtree: fix memory leak in tests
The data is not made a child of the tree: the nodes are made children of the data. So we must explicitly free the data objects.
-rw-r--r--ccan/rbtree/test/run-many.c8
-rw-r--r--ccan/rbtree/test/run.c11
2 files changed, 15 insertions, 4 deletions
diff --git a/ccan/rbtree/test/run-many.c b/ccan/rbtree/test/run-many.c
index a0234030..75d2d01e 100644
--- a/ccan/rbtree/test/run-many.c
+++ b/ccan/rbtree/test/run-many.c
@@ -66,9 +66,9 @@ static void delete_all(trbt_tree_t *rb)
int main(void)
{
trbt_tree_t *rb;
- void *ctx = talloc_init("toplevel");
+ void *ctx = talloc_strdup(NULL, "toplevel");
- plan_tests(7);
+ plan_tests(8);
rb = trbt_create(ctx, 0);
ok1(rb);
@@ -95,6 +95,10 @@ int main(void)
/* All are children of rb, so this is clean. */
talloc_free(rb);
+ /* No memory leaks? */
+ ok1(talloc_total_blocks(ctx) == 1);
+ talloc_free(ctx);
+
/* This exits depending on whether all tests passed */
return exit_status();
}
diff --git a/ccan/rbtree/test/run.c b/ccan/rbtree/test/run.c
index 11d0ed78..5f58fd05 100644
--- a/ccan/rbtree/test/run.c
+++ b/ccan/rbtree/test/run.c
@@ -12,11 +12,11 @@ static void *insert_callback(void *param, void *data)
int main(void)
{
trbt_tree_t *rb;
- void *ctx = talloc_init("toplevel");
+ void *ctx = talloc_strdup(NULL, "toplevel");
char *data, *data2;
/* This is how many tests you plan to run */
- plan_tests(18);
+ plan_tests(19);
rb = trbt_create(ctx, 0);
ok1(rb);
@@ -55,16 +55,23 @@ int main(void)
/* Insert with callback on existing. */
trbt_insert32_callback(rb, 0, insert_callback, data2);
ok1(strcmp(trbt_lookup32(rb, 0), "insert_callback") == 0);
+ talloc_free(data2);
/* Delete. */
+ data2 = trbt_lookup32(rb, 1);
trbt_delete32(rb, 1);
ok1(trbt_lookup32(rb, 1) == NULL);
ok1(trbt_lookup32(rb, 0));
+ talloc_free(data2);
/* This should free everything. */
talloc_free(trbt_lookup32(rb, 0));
talloc_free(rb);
+ /* No memory leaks? */
+ ok1(talloc_total_blocks(ctx) == 1);
+ talloc_free(ctx);
+
/* This exits depending on whether all tests passed */
return exit_status();
}