Fix memory issues in ibusbus.c
authorJames Su <james.su@gmail.com>
Wed, 26 May 2010 19:41:43 +0000 (12:41 -0700)
committerJames Su <james.su@gmail.com>
Wed, 26 May 2010 19:41:43 +0000 (12:41 -0700)
src/ibusbus.c
src/ibusbus.h
src/test-bus.c

index 211080e..a976c9b 100644 (file)
@@ -162,7 +162,7 @@ ibus_bus_connect (IBusBus *bus)
     }
 
     if (priv->connection) {
-        ibus_bus_hello (bus);
+        g_free (ibus_bus_hello (bus));
         g_signal_connect (priv->connection,
                           "destroy",
                           (GCallback) _connection_destroy_cb,
@@ -335,14 +335,14 @@ ibus_bus_create_input_context (IBusBus      *bus,
     return context;
 }
 
-static gboolean
-ibus_bus_call (IBusBus      *bus,
-               const gchar  *name,
-               const gchar  *path,
-               const gchar  *interface,
-               const gchar  *member,
-               GType         first_arg_type,
-               ...)
+IBusMessage *
+ibus_bus_call_with_reply_valist (IBusBus      *bus,
+                                 const gchar  *name,
+                                 const gchar  *path,
+                                 const gchar  *interface,
+                                 const gchar  *member,
+                                 GType         first_arg_type,
+                                 va_list       va_args)
 {
     g_assert (IBUS_IS_BUS (bus));
     g_assert (name != NULL);
@@ -352,8 +352,6 @@ ibus_bus_call (IBusBus      *bus,
 
     IBusMessage *message, *reply;
     IBusError *error;
-    va_list args;
-    GType type;
     gboolean retval;
     IBusBusPrivate *priv;
 
@@ -363,9 +361,7 @@ ibus_bus_call (IBusBus      *bus,
 
     message = ibus_message_new_method_call (name, path, interface, member);
 
-    va_start (args, first_arg_type);
-    ibus_message_append_args_valist (message, first_arg_type, args);
-    va_end (args);
+    ibus_message_append_args_valist (message, first_arg_type, va_args);
 
     reply = ibus_connection_send_with_reply_and_block (
                                         priv->connection,
@@ -377,66 +373,94 @@ ibus_bus_call (IBusBus      *bus,
     if (reply == NULL) {
         g_warning ("%s : %s", error->name, error->message);
         ibus_error_free (error);
-        return FALSE;
+        return NULL;
     }
 
     if ((error = ibus_error_new_from_message (reply)) != NULL) {
         g_warning ("%s : %s", error->name, error->message);
         ibus_error_free (error);
         ibus_message_unref (reply);
-        return FALSE;
+        return NULL;
     }
 
-    va_start (args, first_arg_type);
+    return reply;
+}
 
-    type = first_arg_type;
+IBusMessage *
+ibus_bus_call_with_reply (IBusBus      *bus,
+                          const gchar  *name,
+                          const gchar  *path,
+                          const gchar  *interface,
+                          const gchar  *member,
+                          GType         first_arg_type,
+                          ...)
+{
+    IBusMessage *reply;
+    va_list va_args;
 
-    while (type != G_TYPE_INVALID) {
-        va_arg (args, gpointer);
-        type = va_arg (args, GType);
-    }
+    va_start (va_args, first_arg_type);
+    reply = ibus_bus_call_with_reply_valist (
+        bus, name, path, interface, member, first_arg_type, va_args);
+    va_end (va_args);
 
-    type = va_arg (args, GType);
-    if (type != G_TYPE_INVALID) {
-        retval = ibus_message_get_args_valist (reply, &error, type, args);
-    }
-    else {
-        retval = TRUE;
-    }
-    va_end (args);
+    return reply;
+}
 
-    ibus_message_unref (reply);
+gboolean
+ibus_bus_call (IBusBus      *bus,
+               const gchar  *name,
+               const gchar  *path,
+               const gchar  *interface,
+               const gchar  *member,
+               GType         first_arg_type,
+               ...)
+{
+    IBusMessage *reply;
+    va_list va_args;
 
-    if (!retval) {
-        g_warning ("%s: %s", error->name, error->message);
-        ibus_error_free (error);
-        return FALSE;
+    va_start (va_args, first_arg_type);
+    reply = ibus_bus_call_with_reply_valist (
+        bus, name, path, interface, member, first_arg_type, va_args);
+    va_end (va_args);
+
+    if (reply) {
+      ibus_message_unref (reply);
+      return TRUE;
     }
 
-    return TRUE;
+    return FALSE;
 }
 
-const gchar *
+gchar *
 ibus_bus_current_input_context(IBusBus      *bus)
 {
     g_assert (IBUS_IS_BUS (bus));
+    g_return_val_if_fail (ibus_bus_is_connected (bus), NULL);
 
     gchar *name = NULL;
-    gboolean result;
-
-    result = ibus_bus_call (bus,
-                            IBUS_SERVICE_IBUS,
-                            IBUS_PATH_IBUS,
-                            IBUS_INTERFACE_IBUS,
-                            "CurrentInputContext",
-                            G_TYPE_INVALID,
-                            G_TYPE_STRING, &name,
-                            G_TYPE_INVALID);
+    IBusMessage *reply = NULL;
+    IBusError *error = NULL;
+
+    reply = ibus_bus_call_with_reply (bus,
+                                      IBUS_SERVICE_IBUS,
+                                      IBUS_PATH_IBUS,
+                                      IBUS_INTERFACE_IBUS,
+                                      "CurrentInputContext",
+                                      G_TYPE_INVALID);
+
+    if (reply) {
+        if (ibus_message_get_args (reply, &error, G_TYPE_STRING, &name,
+                                   G_TYPE_INVALID)) {
+            name = g_strdup (name);
+        } else {
+            g_warning ("%s: %s", error->name, error->message);
+            ibus_error_free (error);
+        }
 
-    if (result)
-        return name;
+        ibus_message_unref (reply);
+    }
 
-    return NULL;
+    return name;
 }
 
 static void
@@ -492,27 +516,35 @@ ibus_bus_set_watch_dbus_signal (IBusBus        *bus,
     }
 }
 
-const gchar *
+gchar *
 ibus_bus_hello (IBusBus *bus)
 {
     g_assert (IBUS_IS_BUS (bus));
 
     gchar *unique_name = NULL;
-    gboolean result;
-
-    result = ibus_bus_call (bus,
-                            DBUS_SERVICE_DBUS,
-                            DBUS_PATH_DBUS,
-                            DBUS_INTERFACE_DBUS,
-                            "Hello",
-                            G_TYPE_INVALID,
-                            G_TYPE_STRING, &unique_name,
-                            G_TYPE_INVALID);
+    IBusMessage *reply = NULL;
+    IBusError *error = NULL;
+
+    reply = ibus_bus_call_with_reply (bus,
+                                      DBUS_SERVICE_DBUS,
+                                      DBUS_PATH_DBUS,
+                                      DBUS_INTERFACE_DBUS,
+                                      "Hello",
+                                      G_TYPE_INVALID);
+
+    if (reply) {
+        if (ibus_message_get_args (reply, &error, G_TYPE_STRING, &unique_name,
+                                   G_TYPE_INVALID)) {
+            unique_name = g_strdup (unique_name);
+        } else {
+            g_warning ("%s: %s", error->name, error->message);
+            ibus_error_free (error);
+        }
 
-    if (result)
-        return unique_name;
+        ibus_message_unref (reply);
+    }
 
-    return NULL;
+    return unique_name;
 }
 
 guint
@@ -522,24 +554,30 @@ ibus_bus_request_name (IBusBus      *bus,
 {
     g_assert (IBUS_IS_BUS (bus));
 
-    guint retval;
-    gboolean result;
-
-    result = ibus_bus_call (bus,
-                            DBUS_SERVICE_DBUS,
-                            DBUS_PATH_DBUS,
-                            DBUS_INTERFACE_DBUS,
-                            "RequestName",
-                            G_TYPE_STRING, &name,
-                            G_TYPE_UINT, &flags,
-                            G_TYPE_INVALID,
-                            G_TYPE_UINT, &retval,
-                            G_TYPE_INVALID);
+    IBusMessage *reply = NULL;
+    IBusError *error = NULL;
+    guint retval = 0;
+
+    reply = ibus_bus_call_with_reply (bus,
+                                      DBUS_SERVICE_DBUS,
+                                      DBUS_PATH_DBUS,
+                                      DBUS_INTERFACE_DBUS,
+                                      "RequestName",
+                                      G_TYPE_STRING, &name,
+                                      G_TYPE_UINT, &flags,
+                                      G_TYPE_INVALID);
+
+    if (reply) {
+        if (!ibus_message_get_args (reply, &error, G_TYPE_UINT, &retval,
+                                    G_TYPE_INVALID)) {
+            g_warning ("%s: %s", error->name, error->message);
+            ibus_error_free (error);
+        }
 
-    if (result)
-        return retval;
+        ibus_message_unref (reply);
+    }
 
-    return 0;
+    return retval;
 }
 
 guint
@@ -548,46 +586,60 @@ ibus_bus_release_name (IBusBus      *bus,
 {
     g_assert (IBUS_IS_BUS (bus));
 
-    guint retval;
-    gboolean result;
-
-    result = ibus_bus_call (bus,
-                            DBUS_SERVICE_DBUS,
-                            DBUS_PATH_DBUS,
-                            DBUS_INTERFACE_DBUS,
-                            "ReleaseName",
-                            G_TYPE_STRING, &name,
-                            G_TYPE_INVALID,
-                            G_TYPE_UINT, &retval,
-                            G_TYPE_INVALID);
+    IBusMessage *reply = NULL;
+    IBusError *error = NULL;
+    guint retval = 0;
+
+    reply = ibus_bus_call_with_reply (bus,
+                                      DBUS_SERVICE_DBUS,
+                                      DBUS_PATH_DBUS,
+                                      DBUS_INTERFACE_DBUS,
+                                      "ReleaseName",
+                                      G_TYPE_STRING, &name,
+                                      G_TYPE_INVALID);
+
+    if (reply) {
+        if (!ibus_message_get_args (reply, &error, G_TYPE_UINT, &retval,
+                                    G_TYPE_INVALID)) {
+            g_warning ("%s: %s", error->name, error->message);
+            ibus_error_free (error);
+        }
 
-    if (result)
-        return retval;
+        ibus_message_unref (reply);
+    }
 
-    return 0;
+    return retval;
 }
 
 gboolean
 ibus_bus_name_has_owner (IBusBus        *bus,
                          const gchar    *name)
 {
-    gboolean retval;
-    gboolean result;
+    g_assert (IBUS_IS_BUS (bus));
 
-    result = ibus_bus_call (bus,
-                            DBUS_SERVICE_DBUS,
-                            DBUS_PATH_DBUS,
-                            DBUS_INTERFACE_DBUS,
-                            "NameHasOwner",
-                            G_TYPE_STRING, &name,
-                            G_TYPE_INVALID,
-                            G_TYPE_BOOLEAN, &retval,
-                            G_TYPE_INVALID);
+    IBusMessage *reply = NULL;
+    IBusError *error = NULL;
+    gboolean retval = FALSE;
+
+    reply = ibus_bus_call_with_reply (bus,
+                                      DBUS_SERVICE_DBUS,
+                                      DBUS_PATH_DBUS,
+                                      DBUS_INTERFACE_DBUS,
+                                      "NameHasOwner",
+                                      G_TYPE_STRING, &name,
+                                      G_TYPE_INVALID);
+
+    if (reply) {
+        if (!ibus_message_get_args (reply, &error, G_TYPE_BOOLEAN, &retval,
+                                    G_TYPE_INVALID)) {
+            g_warning ("%s: %s", error->name, error->message);
+            ibus_error_free (error);
+        }
 
-    if (result)
-        return retval;
+        ibus_message_unref (reply);
+    }
 
-    return FALSE;
+    return retval;
 }
 
 GList *
@@ -602,16 +654,13 @@ ibus_bus_add_match (IBusBus     *bus,
 {
     g_assert (IBUS_IS_BUS (bus));
 
-    gboolean result;
-
-    result = ibus_bus_call (bus,
-                            DBUS_SERVICE_DBUS,
-                            DBUS_PATH_DBUS,
-                            DBUS_INTERFACE_DBUS,
-                            "AddMatch",
-                            G_TYPE_STRING, &rule,
-                            G_TYPE_INVALID,
-                            G_TYPE_INVALID);
+    ibus_bus_call (bus,
+                   DBUS_SERVICE_DBUS,
+                   DBUS_PATH_DBUS,
+                   DBUS_INTERFACE_DBUS,
+                   "AddMatch",
+                   G_TYPE_STRING, &rule,
+                   G_TYPE_INVALID);
 }
 
 void
@@ -620,41 +669,46 @@ ibus_bus_remove_match (IBusBus      *bus,
 {
     g_assert (IBUS_IS_BUS (bus));
 
-    gboolean result;
-
-    result = ibus_bus_call (bus,
-                            DBUS_SERVICE_DBUS,
-                            DBUS_PATH_DBUS,
-                            DBUS_INTERFACE_DBUS,
-                            "RemoveMatch",
-                            G_TYPE_STRING, &rule,
-                            G_TYPE_INVALID,
-                            G_TYPE_INVALID);
+    ibus_bus_call (bus,
+                   DBUS_SERVICE_DBUS,
+                   DBUS_PATH_DBUS,
+                   DBUS_INTERFACE_DBUS,
+                   "RemoveMatch",
+                   G_TYPE_STRING, &rule,
+                   G_TYPE_INVALID);
 }
 
-const gchar *
+gchar *
 ibus_bus_get_name_owner (IBusBus        *bus,
                          const gchar    *name)
 {
     g_assert (IBUS_IS_BUS (bus));
 
     gchar *owner = NULL;
-    gboolean result;
-
-    result = ibus_bus_call (bus,
-                            DBUS_SERVICE_DBUS,
-                            DBUS_PATH_DBUS,
-                            DBUS_INTERFACE_DBUS,
-                            "GetNameOwner",
-                            G_TYPE_STRING, &name,
-                            G_TYPE_INVALID,
-                            G_TYPE_STRING, &owner,
-                            G_TYPE_INVALID);
+    IBusMessage *reply = NULL;
+    IBusError *error = NULL;
+
+    reply = ibus_bus_call_with_reply (bus,
+                                      DBUS_SERVICE_DBUS,
+                                      DBUS_PATH_DBUS,
+                                      DBUS_INTERFACE_DBUS,
+                                      "GetNameOwner",
+                                      G_TYPE_STRING, &owner,
+                                      G_TYPE_INVALID);
+
+    if (reply) {
+        if (ibus_message_get_args (reply, &error, G_TYPE_STRING, &owner,
+                                   G_TYPE_INVALID)) {
+            owner = g_strdup (owner);
+        } else {
+            g_warning ("%s: %s", error->name, error->message);
+            ibus_error_free (error);
+        }
 
-    if (result)
-        return owner;
+        ibus_message_unref (reply);
+    }
 
-    return NULL;
+    return owner;
 }
 
 IBusConnection *
@@ -684,7 +738,6 @@ ibus_bus_exit (IBusBus *bus,
                             IBUS_INTERFACE_IBUS,
                             "Exit",
                             G_TYPE_BOOLEAN, &restart,
-                            G_TYPE_INVALID,
                             G_TYPE_INVALID);
     return result;
 }
@@ -704,50 +757,9 @@ ibus_bus_register_component (IBusBus       *bus,
                             IBUS_INTERFACE_IBUS,
                             "RegisterComponent",
                             IBUS_TYPE_COMPONENT, &component,
-                            G_TYPE_INVALID,
                             G_TYPE_INVALID);
 
     return result;
-
-
-#if 0
-    IBusMessage *message, *reply;
-    IBusError *error;
-
-    IBusBusPrivate *priv;
-    priv = IBUS_BUS_GET_PRIVATE (bus);
-
-    message = ibus_message_new_method_call (IBUS_SERVICE_IBUS,
-                                            IBUS_PATH_IBUS,
-                                            IBUS_INTERFACE_IBUS,
-                                            "RegisterComponent");
-
-    ibus_message_append_args (message,
-                              IBUS_TYPE_COMPONENT, &component,
-                              G_TYPE_INVALID);
-
-    reply = ibus_connection_send_with_reply_and_block (
-                                        priv->connection,
-                                        message,
-                                        -1,
-                                        &error);
-    ibus_message_unref (message);
-
-    if (reply == NULL) {
-        g_warning ("%s : %s", error->name, error->message);
-        ibus_error_free (error);
-        return FALSE;
-    }
-
-    if ((error = ibus_error_from_message (reply)) != NULL) {
-        g_warning ("%s : %s", error->name, error->message);
-        ibus_error_free (error);
-        ibus_message_unref (reply);
-        return FALSE;
-    }
-
-    return TRUE;
-#endif
 }
 
 static GList *
index 0a98b73..ff9c7cf 100644 (file)
@@ -103,12 +103,13 @@ IBusConnection
 /**
  * ibus_bus_hello:
  * @bus: An IBusBus.
- * @returns: The unique name of IBus process in DBus.
+ * @returns: The unique name of IBus process in DBus. The return value must be
+ *            freed with g_free().
  *
  * This function sends a "HELLO" message to DBus daemon,
  * which replies the unique name of current IBus process.
  */
-const gchar *ibus_bus_hello             (IBusBus        *bus);
+gchar       *ibus_bus_hello             (IBusBus        *bus);
 
 /**
  * ibus_bus_request_name:
@@ -179,11 +180,11 @@ void         ibus_bus_remove_match      (IBusBus        *bus,
  * ibus_bus_get_name_owner:
  * @bus: An IBusBus.
  * @name: Name.
- * @returns: Owner of the name.
+ * @returns: Owner of the name. The returned value must be freed with g_free().
  *
  * Return the name owner.
  */
-const gchar *ibus_bus_get_name_owner    (IBusBus        *bus,
+gchar       *ibus_bus_get_name_owner    (IBusBus        *bus,
                                          const gchar    *name);
 /* declare ibus methods */
 
@@ -216,12 +217,13 @@ IBusInputContext
 /**
  * ibus_bus_current_input_context:
  * @bus: An IBusBus.
- * @returns: The named of currently focued IBusInputContext if the "CurrentInputContext" call
- *            suceeded, NULL otherwise.
+ * @returns: The named of currently focued IBusInputContext if the
+ *            "CurrentInputContext" call suceeded, NULL otherwise. The return
+ *            value must be freed with g_free().
  *
  * Get the current focused input context.
  */
-const gchar *ibus_bus_current_input_context(IBusBus        *bus);
+gchar       *ibus_bus_current_input_context(IBusBus        *bus);
 
 
 /**
@@ -268,4 +270,3 @@ GList       *ibus_bus_list_active_engines
 
 G_END_DECLS
 #endif
-
index 2721e5b..01d8684 100644 (file)
@@ -57,7 +57,7 @@ int main()
     {
            IBusInputContext *context;
            IBusEngineDesc *engine_desc;
-           const gchar *current_ic;
+           gchar *current_ic;
            context = ibus_bus_create_input_context (bus, "test");
            ibus_input_context_set_capabilities (context, IBUS_CAP_FOCUS);
            ibus_input_context_disable (context);
@@ -74,6 +74,7 @@ int main()
            g_debug ("Test ibusinputcontext.c: passed.");
 
            g_free (active_engine_name);
+           g_free (current_ic);
            g_object_unref (engine_desc);
            g_object_unref (context);
     }