Skip to content

Commit 06f18b1

Browse files
committed
memleak: make notleak() work even before memleak is initalized.
It now simply renames tal names, so it's harmless to do even if we're not going to do memleak detection. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 478a0d5 commit 06f18b1

File tree

3 files changed

+9
-14
lines changed

3 files changed

+9
-14
lines changed

common/configdir.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <ccan/tal/str/str.h>
1010
#include <common/configdir.h>
1111
#include <common/configvar.h>
12+
#include <common/memleak.h>
1213
#include <common/utils.h>
1314
#include <common/version.h>
1415

@@ -35,8 +36,7 @@ static char *opt_set_abspath(const char *arg, char **p)
3536
/* Tal wrappers for opt. */
3637
static void *opt_allocfn(size_t size)
3738
{
38-
return tal_arr_label(NULL, char, size,
39-
TAL_LABEL(opt_allocfn_notleak, ""));
39+
return notleak(tal_arr(NULL, char, size));
4040
}
4141

4242
static void *tal_reallocfn(void *ptr, size_t size)

common/memleak.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@ struct tal_backtrace {
5858
void *notleak_(void *ptr, bool plus_children)
5959
{
6060
const char *name;
61-
/* If we're not tracking, don't do anything. */
62-
if (!memleak_track)
63-
return cast_const(void *, ptr);
6461

6562
/* We use special tal names to mark notleak */
6663
name = tal_name(ptr);
@@ -69,12 +66,14 @@ void *notleak_(void *ptr, bool plus_children)
6966

7067
/* Don't mark more than once! */
7168
if (!strstr(name, "**NOTLEAK")) {
69+
/* Don't use tmpctx: it might not be set up yet! */
7270
if (plus_children)
73-
name = tal_fmt(tmpctx, "%s **NOTLEAK_IGNORE_CHILDREN**",
71+
name = tal_fmt(NULL, "%s **NOTLEAK_IGNORE_CHILDREN**",
7472
name);
7573
else
76-
name = tal_fmt(tmpctx, "%s **NOTLEAK**", name);
74+
name = tal_fmt(NULL, "%s **NOTLEAK**", name);
7775
tal_set_name(ptr, name);
76+
tal_free(name);
7877
}
7978

8079
return cast_const(void *, ptr);
@@ -331,8 +330,7 @@ static void call_memleak_helpers(struct htable *memtable, const tal_t *p)
331330
if (strends(name, "struct memleak_helper")) {
332331
const struct memleak_helper *mh = i;
333332
mh->cb(memtable, p);
334-
} else if (strends(name, " **NOTLEAK**")
335-
|| strends(name, "_notleak")) {
333+
} else if (strends(name, " **NOTLEAK**")) {
336334
memleak_ptr(memtable, i);
337335
memleak_scan_obj(memtable, i);
338336
} else if (strends(name,

common/trace.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <ccan/str/hex/hex.h>
66
#include <ccan/tal/str/str.h>
77
#include <common/json_stream.h>
8+
#include <common/memleak.h>
89
#include <common/pseudorand.h>
910
#include <common/trace.h>
1011
#include <inttypes.h>
@@ -181,15 +182,11 @@ static inline void trace_check_tree(void) {}
181182
static void trace_init(void)
182183
{
183184
const char *dev_trace_file;
184-
const char notleak_name[] = "struct span **NOTLEAK**";
185185

186186
if (active_spans)
187187
return;
188188

189-
active_spans = tal_arrz(NULL, struct span, 1);
190-
/* We're usually too early for memleak to be initialized, so mark
191-
* this notleak manually! */
192-
tal_set_name(active_spans, notleak_name);
189+
active_spans = notleak(tal_arrz(NULL, struct span, 1));
193190

194191
current = NULL;
195192
dev_trace_file = getenv("CLN_DEV_TRACE_FILE");

0 commit comments

Comments
 (0)