From 4a007f5eaf54cd0a04a745eeb54f93d06bdae61f Mon Sep 17 00:00:00 2001
From: Tristan Matthews <tristan.matthews@savoirfairelinux.com>
Date: Fri, 31 May 2013 11:35:53 -0400
Subject: [PATCH] * #25241: gnome: dlclose and free addressbook when exiting

---
 gnome/src/actions.c                  |  2 ++
 gnome/src/contacts/addrbookfactory.c | 21 +++++++++++++++++++--
 gnome/src/contacts/addrbookfactory.h |  1 +
 gnome/src/contacts/addressbook.h     |  1 +
 4 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/gnome/src/actions.c b/gnome/src/actions.c
index f7535fbb74..aafb1839e3 100644
--- a/gnome/src/actions.c
+++ b/gnome/src/actions.c
@@ -158,6 +158,8 @@ sflphone_quit(gboolean force_quit, SFLPhoneClient *client)
         calllist_clean(current_calls_tab);
         calllist_clean(contacts_tab);
         calllist_clean(history_tab);
+        free_addressbook();
+
 #if GLIB_CHECK_VERSION(2,32,0)
         g_application_quit(G_APPLICATION(client));
 #else
diff --git a/gnome/src/contacts/addrbookfactory.c b/gnome/src/contacts/addrbookfactory.c
index 4b4720ff8e..22b733bee5 100644
--- a/gnome/src/contacts/addrbookfactory.c
+++ b/gnome/src/contacts/addrbookfactory.c
@@ -84,21 +84,26 @@ handler_async_search(GList *hits, gpointer user_data)
 
 void abook_init()
 {
+    /* Clear any existing error */
+    dlerror();
+
     const gchar *addrbook_path = PLUGINS_DIR "/libevladdrbook.so";
     /* FIXME: handle should be unloaded with dlclose on exit */
     void *handle = dlopen(addrbook_path, RTLD_LAZY);
 
     if (handle == NULL) {
-        g_debug("Did not load addressbook from path %s", addrbook_path);
+        g_debug("Did not load addressbook from path %s:%s", addrbook_path, dlerror());
         return;
     }
 
     addrbook = g_new0(AddrBookHandle, 1);
+    /* Keep the handle around to dlclose it later */
+    addrbook->handle = handle;
 
 #define LOAD(func) do {                                         \
         addrbook-> func = dlsym(handle, "addressbook_" #func);  \
         if (addrbook-> func == NULL) {                          \
-            g_warning("Couldn't load " # func);                 \
+            g_warning("Couldn't load " # func ":%s", dlerror());\
             dlclose(handle);                                    \
             g_free(addrbook);                                   \
             addrbook = NULL;                                    \
@@ -118,3 +123,15 @@ void abook_init()
 
     addrbook->search_cb = handler_async_search;
 }
+
+void
+free_addressbook()
+{
+    if (!addrbook)
+        return;
+
+    if (addrbook->handle)
+        dlclose(addrbook->handle);
+
+    g_free(addrbook);
+}
diff --git a/gnome/src/contacts/addrbookfactory.h b/gnome/src/contacts/addrbookfactory.h
index 523e44b05e..a03bc85b35 100644
--- a/gnome/src/contacts/addrbookfactory.h
+++ b/gnome/src/contacts/addrbookfactory.h
@@ -35,6 +35,7 @@
 #include "addressbook.h"
 
 void abook_init();
+void free_addressbook();
 
 extern AddrBookHandle *addrbook;
 
diff --git a/gnome/src/contacts/addressbook.h b/gnome/src/contacts/addressbook.h
index bd3b083465..d430e63a28 100644
--- a/gnome/src/contacts/addressbook.h
+++ b/gnome/src/contacts/addressbook.h
@@ -86,6 +86,7 @@ struct AddrBookHandle {
     void (*set_current_book)(const gchar *);
     void (*set_search_type)(AddrbookSearchType);
     void (*search_cb)(GList *, gpointer);
+    void *handle;
 };
 
 #endif
-- 
GitLab