[telepathy-mission-control/master] _mcd_account_connect, _mcd_connection_connect: don't consume @params

Simon McVittie simon.mcvittie at collabora.co.uk
Wed Apr 15 09:23:23 PDT 2009


Instead of passing parameters which are consumed by the function, it seems
safer and more comprehensible to use GHashTable reference counting.

Also, instead of stashing the params on the McdConnection with qdata,
put them in a private struct field (more legible *and* more efficient).

I believe that this branch also fixes a memory leak in
mcd_account_connection_proceed: if plugins refused connection to the
account, the params hash table would be leaked.
---
 src/mcd-account-connection.c |    4 +---
 src/mcd-account.c            |    2 +-
 src/mcd-connection.c         |   22 +++++++++++++++++++---
 3 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/src/mcd-account-connection.c b/src/mcd-account-connection.c
index 525390d..f6fd807 100644
--- a/src/mcd-account-connection.c
+++ b/src/mcd-account-connection.c
@@ -46,9 +46,7 @@ static guint _mcd_account_signal_connection_process = 0;
 void
 _mcd_account_connection_context_free (McdAccountConnectionContext *c)
 {
-    /* params are borrowed from the account, so don't free them.
-     * FIXME: fragile! */
-
+    g_hash_table_unref (c->params);
     g_free (c);
 }
 
diff --git a/src/mcd-account.c b/src/mcd-account.c
index 31bcbbe..0d589fe 100644
--- a/src/mcd-account.c
+++ b/src/mcd-account.c
@@ -2528,7 +2528,7 @@ _mcd_account_set_connection_context (McdAccount *self,
 {
     g_return_if_fail (MCD_IS_ACCOUNT (self));
 
-    if (self->priv->connection_context == NULL)
+    if (self->priv->connection_context != NULL)
     {
         _mcd_account_connection_context_free (self->priv->connection_context);
     }
diff --git a/src/mcd-connection.c b/src/mcd-connection.c
index 8e00bda..7f7ade0 100644
--- a/src/mcd-connection.c
+++ b/src/mcd-connection.c
@@ -89,6 +89,7 @@ struct _McdConnectionPrivate
 
     /* Account */
     McdAccount *account;
+    GHashTable *params;
 
     /* Associated profile */
     /* McProfile *profile; */
@@ -1618,6 +1619,12 @@ _mcd_connection_finalize (GObject * object)
     if (priv->recognized_presences)
         g_hash_table_destroy (priv->recognized_presences);
 
+    if (priv->params != NULL)
+    {
+        g_hash_table_unref (priv->params);
+        priv->params = NULL;
+    }
+
     G_OBJECT_CLASS (mcd_connection_parent_class)->finalize (object);
 }
 
@@ -2430,7 +2437,7 @@ mcd_connection_close (McdConnection *connection)
  * @connection: the #McdConnection.
  * @params: a #GHashTable of connection parameters.
  *
- * Activate @connection. The connection takes ownership of @params.
+ * Activate @connection. The connection references @params, if non-NULL.
  */
 void
 _mcd_connection_connect (McdConnection *connection, GHashTable *params)
@@ -2454,13 +2461,22 @@ _mcd_connection_connect (McdConnection *connection, GHashTable *params)
     if (mcd_connection_get_connection_status (connection) ==
         TP_CONNECTION_STATUS_DISCONNECTED)
     {
-        g_object_set_data_full ((GObject *)connection, "params", params,
-                                (GDestroyNotify)g_hash_table_destroy);
+        if (connection->priv->params != NULL)
+        {
+            g_hash_table_unref (connection->priv->params);
+            connection->priv->params = NULL;
+        }
 
         if (params)
+        {
+            connection->priv->params = g_hash_table_ref (params);
             _mcd_connection_connect_with_params (connection, params);
+        }
         else
+        {
+            connection->priv->params = NULL;
             _mcd_account_connection_begin (priv->account);
+        }
     }
     else
     {
-- 
1.5.6.5




More information about the telepathy-commits mailing list