diff --git a/common/daemon.c b/common/daemon.c index 69d1f5762973..6ed789a50712 100644 --- a/common/daemon.c +++ b/common/daemon.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -126,6 +127,12 @@ void daemon_setup(const char *argv0, crashlog_activate(); #endif +#if DEVELOPER + /* This has significant overhead, so we only enable it if told */ + if (getenv("LIGHTNINGD_DEV_MEMLEAK")) + memleak_init(); +#endif + /* We handle write returning errors! */ signal(SIGPIPE, SIG_IGN); wally_init(0); @@ -137,6 +144,9 @@ void daemon_setup(const char *argv0, void daemon_shutdown(void) { +#if DEVELOPER + memleak_cleanup(); +#endif tal_free(tmpctx); wally_cleanup(0); } diff --git a/connectd/Makefile b/connectd/Makefile index 1a7aa86f9b05..f624b441d4cf 100644 --- a/connectd/Makefile +++ b/connectd/Makefile @@ -49,6 +49,7 @@ CONNECTD_COMMON_OBJS := \ common/dev_disconnect.o \ common/features.o \ common/gen_status_wire.o \ + common/memleak.o \ common/msg_queue.o \ common/pseudorand.o \ common/status.o \ diff --git a/gossipd/Makefile b/gossipd/Makefile index bac6be1cf0ae..6f03f5121463 100644 --- a/gossipd/Makefile +++ b/gossipd/Makefile @@ -49,6 +49,7 @@ GOSSIPD_COMMON_OBJS := \ common/dev_disconnect.o \ common/features.o \ common/gen_status_wire.o \ + common/memleak.o \ common/msg_queue.o \ common/ping.o \ common/pseudorand.o \ diff --git a/hsmd/Makefile b/hsmd/Makefile index ad3eff3b850a..f13a6e5b7584 100644 --- a/hsmd/Makefile +++ b/hsmd/Makefile @@ -21,6 +21,7 @@ HSMD_COMMON_OBJS := \ common/gen_status_wire.o \ common/hash_u5.o \ common/key_derive.o \ + common/memleak.o \ common/msg_queue.o \ common/permute_tx.o \ common/status.o \ diff --git a/lightningd/lightningd.c b/lightningd/lightningd.c index e4a830d6a04e..4c02f3c32bd1 100644 --- a/lightningd/lightningd.c +++ b/lightningd/lightningd.c @@ -57,7 +57,6 @@ /*~ This is common code: routines shared by one or more executables * (separate daemons, or the lightning-cli program). */ #include -#include #include #include #include @@ -116,15 +115,6 @@ static struct lightningd *new_lightningd(const tal_t *ctx) ld->dev_disconnect_fd = -1; ld->dev_subdaemon_fail = false; ld->dev_allow_localhost = false; - - /*~ Behaving differently depending on environment variables is a hack, - * *but* hacks are allowed for dev-mode stuff. In this case, there's - * a significant overhead to the memory leak detection stuff, and we - * can't use it under valgrind (an awesome runtime memory usage - * detector for C and C++ programs), so the test harness uses this var - * to disable it in that case. */ - if (getenv("LIGHTNINGD_DEV_MEMLEAK")) - memleak_init(); #endif /*~ These are CCAN lists: an embedded double-linked list. It's not @@ -374,9 +364,10 @@ static const char *find_daemon_dir(const tal_t *ctx, const char *argv0) return find_my_pkglibexec_path(ctx, take(my_path)); } -/*~ We like to free everything on exit, so valgrind doesn't complain. In some - * ways it would be neater not to do this, but it turns out some transient - * objects still need cleaning. */ +/*~ We like to free everything on exit, so valgrind doesn't complain (valgrind + * is an awesome runtime memory usage detector for C and C++ programs). In + * some ways it would be neater not to do this, but it turns out some + * transient objects still need cleaning. */ static void shutdown_subdaemons(struct lightningd *ld) { struct peer *p; @@ -798,9 +789,6 @@ int main(int argc, char *argv[]) tal_free(ld); opt_free_table(); -#if DEVELOPER - memleak_cleanup(); -#endif daemon_shutdown(); /*~ Farewell. Next stop: hsmd/hsmd.c. */