>From 8a9ec118fc6ffddb6778315c4acc610d84621a4f Mon Sep 17 00:00:00 2001
From: root <root@localhost.localdomain>
Date: Tue, 15 Jun 2010 16:46:30 +0200
Subject: [PATCH] Memleaks removal

---
 README                             |    9 +++++++++
 src/Daemon/CommLayerServerDBus.cpp |    3 ++-
 src/Daemon/Daemon.cpp              |   35 +++++++++++++++++++++++++----------
 3 files changed, 36 insertions(+), 11 deletions(-)

diff --git a/README b/README
index c62fce4..26cbbcb 100644
--- a/README
+++ b/README
@@ -1,2 +1,11 @@
 These sources are in early stages. They are changing every day :-)...
 Anyway, patches are welcome.
+
+** Using Valgrind
+
+When running ABRT under memcheck, GLib's environment variables should
+be set to turn off glib's memory optimization, so valgrind is not
+confused:
+
+G_SLICE=always-malloc G_DEBUG=gc-friendly valgrind --tool=memcheck \
+  --leak-check=full abrtd -dvvv
diff --git a/src/Daemon/CommLayerServerDBus.cpp b/src/Daemon/CommLayerServerDBus.cpp
index d45209a..d72968f 100644
--- a/src/Daemon/CommLayerServerDBus.cpp
+++ b/src/Daemon/CommLayerServerDBus.cpp
@@ -597,6 +597,7 @@ CCommLayerServerDBus::CCommLayerServerDBus()
     //
     // and need to code up handle_message to check for "Disconnected" dbus signal
 
+    /* Also sets g_dbus_conn to conn. */
     attach_dbus_conn_to_glib_main_loop(conn, "/com/redhat/abrt", message_received);
 
     VERB3 log("dbus_bus_request_name");
@@ -619,5 +620,5 @@ CCommLayerServerDBus::CCommLayerServerDBus()
 
 CCommLayerServerDBus::~CCommLayerServerDBus()
 {
-// do we need to do anything here?
+    dbus_connection_unref(g_dbus_conn);
 }
diff --git a/src/Daemon/Daemon.cpp b/src/Daemon/Daemon.cpp
index cd02350..ad5ae84 100644
--- a/src/Daemon/Daemon.cpp
+++ b/src/Daemon/Daemon.cpp
@@ -487,6 +487,8 @@ static void run_main_loop(GMainLoop* loop)
         if (some_ready)
             g_main_context_dispatch(context);
     }
+
+    g_main_context_unref(context);
 }
 
 static void start_syslog_logging()
@@ -628,8 +630,10 @@ int main(int argc, char** argv)
     }
 
     GMainLoop* pMainloop = NULL;
-    GIOChannel* pGiochannel_inotify = NULL;
-    GIOChannel* pGiochannel_signal = NULL;
+    GIOChannel* channel_inotify = NULL;
+    guint channel_inotify_event_id = 0;
+    GIOChannel* channel_signal = NULL;
+    guint channel_signal_event_id = 0;
     bool lockfile_created = false;
     bool pidfile_created = false;
     CCrashWatcher watcher;
@@ -673,13 +677,20 @@ int main(int argc, char** argv)
             throw 1;
 
         VERB1 log("Adding inotify watch to glib main loop");
-        pGiochannel_inotify = g_io_channel_unix_new(inotify_fd);
-        g_io_add_watch(pGiochannel_inotify, G_IO_IN, handle_inotify_cb, NULL);
+        channel_inotify = g_io_channel_unix_new(inotify_fd);
+        channel_inotify_event_id = g_io_add_watch(channel_inotify,
+                                                  G_IO_IN,
+                                                  handle_inotify_cb,
+                                                  NULL);
+
         /* Add an event source which waits for INT/TERM signal */
 
         VERB1 log("Adding signal pipe watch to glib main loop");
-        pGiochannel_signal = g_io_channel_unix_new(s_signal_pipe[0]);
-        g_io_add_watch(pGiochannel_signal, G_IO_IN, handle_signal_cb, NULL);
+        channel_signal = g_io_channel_unix_new(s_signal_pipe[0]);
+        channel_signal_event_id = g_io_add_watch(channel_signal,
+                                                G_IO_IN,
+                                                handle_signal_cb,
+                                                NULL);
 
         /* Mark the territory */
         VERB1 log("Creating lock file");
@@ -752,10 +763,14 @@ int main(int argc, char** argv)
     if (lockfile_created)
         unlink(VAR_RUN_LOCK_FILE);
 
-    if (pGiochannel_signal)
-        g_io_channel_unref(pGiochannel_signal);
-    if (pGiochannel_inotify)
-        g_io_channel_unref(pGiochannel_inotify);
+    if (channel_signal_event_id > 0)
+        g_source_remove(channel_signal_event_id);
+    if (channel_signal)
+        g_io_channel_unref(channel_signal);
+    if (channel_inotify_event_id > 0)
+        g_source_remove(channel_inotify_event_id);
+    if (channel_inotify)
+        g_io_channel_unref(channel_inotify);
 
     cron_shutdown();
 
-- 
1.6.6.1

