tal: don't automatically register cleanup function.
authorRusty Russell <rusty@rustcorp.com.au>
Mon, 17 Dec 2012 03:25:42 +0000 (13:55 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Mon, 17 Dec 2012 03:25:42 +0000 (13:55 +1030)
It may interfere with other at_exit() calls, so let them call it manually.
Also, use memset to zero, which really does make valgrind notice any leaks.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
19 files changed:
ccan/tal/tal.c
ccan/tal/tal.h
ccan/tal/test/run-allocfail.c
ccan/tal/test/run-array.c
ccan/tal/test/run-count.c
ccan/tal/test/run-destructor.c
ccan/tal/test/run-expand.c
ccan/tal/test/run-free.c
ccan/tal/test/run-groups-grow.c
ccan/tal/test/run-iter.c
ccan/tal/test/run-named-debug.c
ccan/tal/test/run-named-nolabels.c
ccan/tal/test/run-named.c
ccan/tal/test/run-notifier.c
ccan/tal/test/run-overflow.c
ccan/tal/test/run-steal.c
ccan/tal/test/run-take.c
ccan/tal/test/run-test-backend.c
ccan/tal/test/run.c

index ce1220701e6640fd43357af0d427c4aa6a380883..1934a01318a3f09060614e144d189fff8467469b 100644 (file)
@@ -76,7 +76,6 @@ static void *(*allocfn)(size_t size) = malloc;
 static void *(*resizefn)(void *, size_t size) = realloc;
 static void (*freefn)(void *) = free;
 static void (*errorfn)(const char *msg) = (void *)abort;
-static bool initialized = false;
 /* Count on non-destrutor notifiers; often stays zero. */
 static size_t notifiers = 0;
 
@@ -101,23 +100,19 @@ static struct children *ignore_destroying_bit(struct children *parent_child)
 }
 
 /* This means valgrind can see leaks. */
-static void tal_cleanup(void)
+void tal_cleanup(void)
 {
        struct tal_hdr *i;
 
-       while ((i = list_top(&null_parent.c.children, struct tal_hdr, list)))
+       while ((i = list_top(&null_parent.c.children, struct tal_hdr, list))) {
                list_del(&i->list);
+               memset(i, 0, sizeof(*i));
+       }
 
        /* Cleanup any taken pointers. */
        take_cleanup();
 }
 
-/* For allocation failures inside ccan/take */
-static void take_alloc_failed(const void *p)
-{
-       tal_free(p);
-}
-
 /* We carefully start all real properties with a zero byte. */
 static bool is_literal(const struct prop_hdr *prop)
 {
@@ -344,11 +339,6 @@ static bool add_child(struct tal_hdr *parent, struct tal_hdr *child)
        struct children *children = find_property(parent, CHILDREN);
 
         if (!children) {
-               if (unlikely(!initialized)) {
-                       atexit(tal_cleanup);
-                       take_allocfail(take_alloc_failed);
-                       initialized = true;
-               }
                children = add_child_property(parent, child);
                if (!children)
                        return false;
index 28be3f2034c133b276bc1a9e7f803df6fa033469..86b56d35a3c26280a9915216a5443ebef78626e1 100644 (file)
@@ -327,6 +327,17 @@ void tal_set_backend(void *(*alloc_fn)(size_t size),
        tal_expand_((void **)(a1p), (a2), sizeof**(a1p),        \
                    (num2) + 0*sizeof(*(a1p) == (a2)))
 
+/**
+ * tal_cleanup - remove pointers from NULL node
+ *
+ * Internally, tal keeps a list of nodes allocated from @ctx NULL; this
+ * prevents valgrind from noticing memory leaks.  This re-initializes
+ * that list to empty.
+ *
+ * It also calls take_cleanup() for you.
+ */
+void tal_cleanup(void);
+
 
 /**
  * tal_check - set the allocation or error functions to use
index a0910cac5441dcf4fa5097798610134aa34b07c5..a166be3f6520a8795ea2c7bd916f0bfdf2a9cada 100644 (file)
@@ -148,5 +148,6 @@ int main(void)
        ok1(err_count == when_to_fail - 1);
 
        tal_free(p);
+       tal_cleanup();
        return exit_status();
 }
index 6212dae52ced2163dedeece78294d1023f36f333..d3001faff84178746ff9dced2768b2f3ed296412 100644 (file)
@@ -42,5 +42,6 @@ int main(void)
        ok1(tal_first(parent) == NULL);
        tal_free(parent);
 
+       tal_cleanup();
        return exit_status();
 }
index 91b020dcc8de13138d2e15c6683db1fce4c5c7e0..6a4eb4ab8ada57a9aef4d772fedf61160e6662a4 100644 (file)
@@ -82,5 +82,7 @@ int main(void)
                tal_free(p2);
                tal_free(p1);
        }
+
+       tal_cleanup();
        return exit_status();
 }
index 30c5136b5b4452aba1fe7f49cb5799f60cd88a80..873548889e91472013c3901b92bae8f01ae4e25c 100644 (file)
@@ -63,5 +63,6 @@ int main(void)
        tal_free(parent);
        ok1(destroy_count == 4);
 
+       tal_cleanup();
        return exit_status();
 }
index 2edb31f6951766d69893bad05983bcddae37636a..607947bea70fe477dfdae3fe9f35388a3f52045a 100644 (file)
@@ -28,5 +28,6 @@ int main(void)
 
        tal_free(a);
 
+       tal_cleanup();
        return exit_status();
 }
index 7b9a086e7c908bfe17b977d696ac0f6c85a0f7fe..29aa8c6f49dacf462300f41eda207b414c29ea95 100644 (file)
@@ -21,5 +21,6 @@ int main(void)
        tal_free(p);
        ok1(errno == EINVAL);
 
+       tal_cleanup();
        return exit_status();
 }
index e8e382db75ded48ebc5b073015db02dc47bbaaa8..ea379c0836adc152191fbc4ee63633ee85e5eff4 100644 (file)
@@ -42,5 +42,7 @@ int main(void)
 
        /* We can expect some residue from having any child, but limited! */
        ok1(num_allocated <= allocated_after_first);
+       tal_free(p);
+       tal_cleanup();
        return exit_status();
 }
index 7021be27555df5705b43a85ecf69bbb317a1ff77..561e09cc2e697af14d25b7127388e35543c75106 100644 (file)
@@ -29,5 +29,6 @@ int main(void)
                ok1(*p[i] == '1');
                tal_free(p[i]);
        }
+       tal_cleanup();
        return exit_status();
 }
index a6e51b4391167148c4cbc77c1173e28cf9eaf9db..679e6ec8df4117f3690f16db9a61a50673b6103b 100644 (file)
@@ -30,5 +30,6 @@ int main(void)
        ok1(strcmp(tal_name(p), __FILE__ ":29:int[]") == 0);
        tal_free(p);
 
+       tal_cleanup();
        return exit_status();
 }
index 512bee3f970837643291e52aeceb2a7ea0cf92f5..fc7b81f527b7d9f9e718c085ebc16bd160764fcd 100644 (file)
@@ -26,5 +26,6 @@ int main(void)
 
        tal_free(p);
 
+       tal_cleanup();
        return exit_status();
 }
index acdc4513c2e9e83e82535e06a7f03d608a6123bc..d6275ac687c3f8d111658e40fa40c27312037f57 100644 (file)
@@ -29,5 +29,6 @@ int main(void)
        ok1(strcmp(tal_name(p), "int[]") == 0);
        tal_free(p);
 
+       tal_cleanup();
        return exit_status();
 }
index f085b8255852bcf29f3ba56e0f32646853aac29a..b57c902e80ba9c3b7ec6a9ea285b4c0be0b40419 100644 (file)
@@ -126,5 +126,6 @@ int main(void)
        tal_del_notifier(new_ctx, resize_notifier);
        tal_free(new_ctx);
 
+       tal_cleanup();
        return exit_status();
 }
index c5daf97d2794b99966dd59e959cdab7d649be87f..473ba70a11b775160c88a3af8ee43291572955a9 100644 (file)
@@ -94,5 +94,6 @@ int main(void)
        ok1(error_count == 3);
        tal_free(origpi);
 
+       tal_cleanup();
        return exit_status();
 }
index 3ff7b2b0b88a25c8bb491480ec07f90bb39b9085..36251cb7b249914077663d3b42185e5e1f33fdbb 100644 (file)
@@ -36,5 +36,6 @@ int main(void)
        ok1(tal_parent(p[4]) == p[0]);
        tal_free(p[0]);
 
+       tal_cleanup();
        return exit_status();
 }
index 72d2e8e3cabd2878377a4a03fbbef960777dd2eb..94b6581749964b3331f13907d3eaaececade2754 100644 (file)
@@ -52,5 +52,6 @@ int main(void)
        ok1(tal_dup(NULL, char, take(c), 5, 5) == NULL);
        ok1(!taken_any());
 
+       tal_cleanup();
        return exit_status();
 }
index 66144cb3a7356efdf99fba0524acc2cb0d8831ab..8fdfc064abb7eca68d06a18d0f4b00f7b9927df3 100644 (file)
@@ -75,5 +75,6 @@ int main(void)
        /* Finally, free the parent. */
        tal_free(p);
 
+       tal_cleanup();
        return exit_status();
 }
index ab4341f2c93f443bfdc6b61f84ce57c4fa45be2b..984865125deca6538ea531b714a077dce3a05b87 100644 (file)
@@ -56,5 +56,6 @@ int main(void)
        }
        tal_free(parent);
 
+       tal_cleanup();
        return exit_status();
 }