From c91d8429a01725dd881abe7014fbf5c1fdba07d6 Mon Sep 17 00:00:00 2001 From: James Su Date: Wed, 26 May 2010 14:56:15 -0700 Subject: [PATCH] Fix memory issues in ibusconnection.* and ibusproxy.c --- src/ibusconnection.c | 90 +++++++++++++++++++++++++++++++++------------------- src/ibusconnection.h | 30 ++++++++++++++++-- src/ibusproxy.c | 40 ++++++++++++----------- 3 files changed, 106 insertions(+), 54 deletions(-) diff --git a/src/ibusconnection.c b/src/ibusconnection.c index 21de213..9cf7e16 100644 --- a/src/ibusconnection.c +++ b/src/ibusconnection.c @@ -722,15 +722,15 @@ ibus_connection_send_with_reply_and_block (IBusConnection *connection, return reply; } -gboolean -ibus_connection_call (IBusConnection *connection, - const gchar *name, - const gchar *path, - const gchar *interface, - const gchar *member, - IBusError **error, - GType first_arg_type, - ...) +static IBusMessage * +ibus_connection_call_with_reply_valist (IBusConnection *connection, + const gchar *name, + const gchar *path, + const gchar *interface, + const gchar *member, + IBusError **error, + GType first_arg_type, + va_list va_args) { g_assert (IBUS_IS_CONNECTION (connection)); g_assert (name != NULL); @@ -744,17 +744,12 @@ ibus_connection_call (IBusConnection *connection, IBusMessage *message, *reply; IBusError *tmp_error; - va_list args; GType type; gboolean retval; 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 ( connection, @@ -764,7 +759,7 @@ ibus_connection_call (IBusConnection *connection, ibus_message_unref (message); if (reply == NULL) { - return FALSE; + return NULL; } if ((tmp_error = ibus_error_new_from_message (reply)) != NULL) { @@ -775,30 +770,59 @@ ibus_connection_call (IBusConnection *connection, ibus_error_free (tmp_error); } ibus_message_unref (reply); - return FALSE; + return NULL; } - va_start (args, first_arg_type); + return reply; +} + +IBusMessage * +ibus_connection_call_with_reply (IBusConnection *connection, + const gchar *name, + const gchar *path, + const gchar *interface, + const gchar *member, + IBusError **error, + GType first_arg_type, + ...) +{ + IBusMessage *reply; + va_list va_args; - type = first_arg_type; + va_start (va_args, first_arg_type); + reply = ibus_connection_call_with_reply_valist ( + connection, name, path, interface, member, error, + first_arg_type, va_args); + va_end (va_args); - while (type != G_TYPE_INVALID) { - va_arg (args, gpointer); - type = va_arg (args, GType); - } - type = va_arg (args, GType); + return reply; +} - if (type != G_TYPE_INVALID) { - retval = ibus_message_get_args_valist (reply, error, type, args); - } - else { - retval = TRUE; - } +gboolean +ibus_connection_call (IBusConnection *connection, + const gchar *name, + const gchar *path, + const gchar *interface, + const gchar *member, + IBusError **error, + GType first_arg_type, + ...) +{ + IBusMessage *reply; + va_list va_args; - va_end (args); - ibus_message_unref (reply); + va_start (va_args, first_arg_type); + reply = ibus_connection_call_with_reply_valist ( + connection, name, path, interface, member, error, + first_arg_type, va_args); + va_end (va_args); - return retval; + if (reply) { + ibus_message_unref (reply); + return TRUE; + } + + return FALSE; } void diff --git a/src/ibusconnection.h b/src/ibusconnection.h index e4774c3..14bcc4a 100644 --- a/src/ibusconnection.h +++ b/src/ibusconnection.h @@ -429,7 +429,8 @@ IBusMessage *ibus_connection_send_with_reply_and_block * @...: Rest of arguments, NULL to mark the end. * @returns: TRUE if succeed; FALSE otherwise. * - * Invoke a member function by sending an IBusMessage. + * Invoke a member function by sending an IBusMessage. This method does not + * support reply message, use ibus_connection_call_with_reply instead. * * @see_also: ibus_connection_send_valist(). */ @@ -443,6 +444,32 @@ gboolean ibus_connection_call (IBusConnection *connect ...); /** + * ibus_connection_call_with_reply: + * @connection: An IBusConnection. + * @name: Name of the signal. + * @path: The path to the object emitting the signal. + * @interface: The interface the signal is emitted from. + * @member: The name of the member function to be called. + * @error: Returned error is stored here; NULL to ignore error. + * @first_arg_type: Type of first argument. + * @...: Rest of arguments, NULL to mark the end. + * @returns: Reply message, or NULL when fail. The returned message must be + * freed with ibus_message_unref(). + * + * Invoke a member function by sending an IBusMessage. + * + * @see_also: ibus_connection_send_valist(). + */ +IBusMessage *ibus_connection_call_with_reply (IBusConnection *connection, + const gchar *name, + const gchar *path, + const gchar *interface, + const gchar *member, + IBusError **error, + GType first_arg_type, + ...); + +/** * ibus_connection_flush: * @connection: An IBusConnection. * @@ -490,4 +517,3 @@ gboolean ibus_connection_unregister_object_path G_END_DECLS #endif - diff --git a/src/ibusproxy.c b/src/ibusproxy.c index 6ff1560..1ac7e0d 100644 --- a/src/ibusproxy.c +++ b/src/ibusproxy.c @@ -264,7 +264,6 @@ ibus_proxy_constructor (GType type, "AddMatch", &error, G_TYPE_STRING, &rule, - G_TYPE_INVALID, G_TYPE_INVALID)) { g_warning ("%s: %s", error->name, error->message); ibus_error_free (error); @@ -284,7 +283,6 @@ ibus_proxy_constructor (GType type, "AddMatch", &error, G_TYPE_STRING, &rule, - G_TYPE_INVALID, G_TYPE_INVALID)) { g_warning ("%s: %s", error->name, error->message); ibus_error_free (error); @@ -351,7 +349,6 @@ ibus_proxy_destroy (IBusProxy *proxy) "RemoveMatch", &error, G_TYPE_STRING, &rule, - G_TYPE_INVALID, G_TYPE_INVALID)) { g_warning ("%s: %s", error->name, error->message); @@ -372,7 +369,6 @@ ibus_proxy_destroy (IBusProxy *proxy) "RemoveMatch", &error, G_TYPE_STRING, &rule, - G_TYPE_INVALID, G_TYPE_INVALID)) { g_warning ("%s: %s", error->name, error->message); @@ -520,22 +516,28 @@ ibus_proxy_get_unique_name (IBusProxy *proxy) priv = IBUS_PROXY_GET_PRIVATE (proxy); if (priv->unique_name == NULL && priv->connection != NULL) { - IBusError *error; - gchar *owner; - if (!ibus_connection_call (priv->connection, - DBUS_SERVICE_DBUS, - DBUS_PATH_DBUS, - DBUS_INTERFACE_DBUS, - "GetNameOwner", - &error, - G_TYPE_STRING, &(priv->name), - G_TYPE_INVALID, - G_TYPE_STRING, &owner, - G_TYPE_INVALID)) { - g_warning ("%s: %s", error->name, error->message); - ibus_error_free (error); + IBusMessage *reply = NULL; + IBusError *error = NULL; + gchar *owner = NULL; + reply = ibus_connection_call_with_reply (priv->connection, + DBUS_SERVICE_DBUS, + DBUS_PATH_DBUS, + DBUS_INTERFACE_DBUS, + "GetNameOwner", + NULL, + G_TYPE_STRING, &(priv->name), + G_TYPE_INVALID); + if (reply) { + if (ibus_message_get_args (reply, &error, G_TYPE_STRING, &owner, + G_TYPE_INVALID)) { + priv->unique_name = g_strdup (owner); + } else { + g_warning ("%s: %s", error->name, error->message); + ibus_error_free (error); + } + + ibus_message_unref (reply); } - priv->unique_name = g_strdup (owner); } return priv->unique_name; -- 2.7.4