[PATCH v2 3/8] rfkill: Fix memory leak in rfkill_exit
From: Szymon Janc <hidden>
Date: 2012-11-30 12:57:39
Subsystem:
the rest · Maintainer:
Linus Torvalds
g_io_add_watch increase channel ref count but g_io_channel_shutdown
doesn't drop reference nor remove watch. Since close on unref is set
for channel and it is watch we are interested keep watch id and not
channel id and remove watch on exit.
120 bytes in 1 blocks are still reachable in loss record 181 of 235
at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
by 0x4E7FA78: g_malloc (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.3)
by 0x4EB66F4: g_io_channel_unix_new (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.3)
by 0x167B8D: rfkill_init (rfkill.c:157)
by 0x1215E4: main (main.c:540)
6 bytes in 1 blocks are still reachable in loss record 12 of 235
at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
by 0x4E7FA78: g_malloc (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.3)
by 0x4E942DD: g_strdup (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.3)
by 0x4E6CF95: g_io_channel_init (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.3)
by 0x4EB66FF: g_io_channel_unix_new (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.3)
by 0x167B8D: rfkill_init (rfkill.c:157)
by 0x1215E4: main (main.c:540)
---
src/rfkill.c | 18 ++++++++++--------
1 file changed, 10 insertions(+), 8 deletions(-)
diff --git a/src/rfkill.c b/src/rfkill.c
index b40c6e7..f82596b 100644
--- a/src/rfkill.c
+++ b/src/rfkill.c@@ -139,11 +139,12 @@ static gboolean rfkill_event(GIOChannel *chan, return TRUE; } -static GIOChannel *channel = NULL; +static guint watch = 0; void rfkill_init(void) { int fd; + GIOChannel *channel; if (!main_opts.remember_powered) return;
@@ -157,17 +158,18 @@ void rfkill_init(void) channel = g_io_channel_unix_new(fd); g_io_channel_set_close_on_unref(channel, TRUE); - g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR, - rfkill_event, NULL); + watch = g_io_add_watch(channel, + G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR, + rfkill_event, NULL); + + g_io_channel_unref(channel); } void rfkill_exit(void) { - if (!channel) + if (watch == 0) return; - g_io_channel_shutdown(channel, TRUE, NULL); - g_io_channel_unref(channel); - - channel = NULL; + g_source_remove(watch); + watch = 0; }
--
1.7.9.5