Fix to unref DBusConnection properly 22/134222/1
authorSangyoon Jang <jeremy.jang@samsung.com>
Thu, 15 Jun 2017 09:16:46 +0000 (18:16 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Thu, 15 Jun 2017 09:16:46 +0000 (18:16 +0900)
Change-Id: I358a9f84efdcb27d7f03aa20c89d902e3e5c7b1d
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
src/common/tzip_interface.cc

index 971f1c6..0d09569 100644 (file)
@@ -28,6 +28,10 @@ const int kTzipMountMaximumRetryCount = 15;
 
 }  // namespace
 
+void DBusConnectionDeleter(DBusConnection* conn) {
+  dbus_connection_unref(conn);
+}
+
 void DBusMessageDeleter(DBusMessage* message) {
   dbus_message_unref(message);
 }
@@ -47,7 +51,8 @@ class TzipInterface::Pimpl {
   bool MountZip(const boost::filesystem::path& zip_path) {
     zip_path_ = zip_path;
 
-    DBusConnection *conn = dbus_bus_get(DBUS_BUS_SYSTEM, nullptr);
+    std::unique_ptr<DBusConnection, void(*)(DBusConnection*)> conn(
+        dbus_bus_get(DBUS_BUS_SYSTEM, nullptr), DBusConnectionDeleter);
     if (!conn) {
       return false;
     }
@@ -80,7 +85,7 @@ class TzipInterface::Pimpl {
       return false;
     }
 
-    if (dbus_connection_send(conn, msg.get(), nullptr) == FALSE) {
+    if (dbus_connection_send(conn.get(), msg.get(), nullptr) == FALSE) {
       LOG(ERROR) << "Could not send DBUS message when mounting zip file";
       return false;
     }
@@ -90,7 +95,8 @@ class TzipInterface::Pimpl {
   }
 
   bool UnmountZip() {
-    DBusConnection *conn = dbus_bus_get(DBUS_BUS_SYSTEM, nullptr);
+    std::unique_ptr<DBusConnection, void(*)(DBusConnection*)> conn(
+        dbus_bus_get(DBUS_BUS_SYSTEM, nullptr), DBusConnectionDeleter);
     if (!conn) {
       return false;
     }
@@ -112,7 +118,7 @@ class TzipInterface::Pimpl {
       return false;
     }
 
-    if (dbus_connection_send(conn, msg.get(), nullptr) == FALSE) {
+    if (dbus_connection_send(conn.get(), msg.get(), nullptr) == FALSE) {
       LOG(ERROR) << "Could not send DBUS message when unmounting zip file";
       return false;
     }
@@ -130,7 +136,8 @@ class TzipInterface::Pimpl {
     std::unique_ptr<DBusError, void(*)(DBusError*)> err(new DBusError(),
                                                         DBusErrorDeleter);
 
-    DBusConnection* conn = dbus_bus_get(DBUS_BUS_SYSTEM, nullptr);
+    std::unique_ptr<DBusConnection, void(*)(DBusConnection*)> conn(
+        dbus_bus_get(DBUS_BUS_SYSTEM, nullptr), DBusConnectionDeleter);
     if (!conn) {
       *success = false;
       return false;
@@ -154,8 +161,8 @@ class TzipInterface::Pimpl {
     }
 
     dbus_error_init(err.get());
-    reply = dbus_connection_send_with_reply_and_block(conn, msg.get(), 500,
-                                                      err.get());
+    reply = dbus_connection_send_with_reply_and_block(conn.get(), msg.get(),
+                                                      500, err.get());
     if (!reply) {
       *success = false;
       return false;