GDBus: plug some memory leaks
authorDavid Zeuthen <davidz@redhat.com>
Wed, 30 Jun 2010 20:36:47 +0000 (16:36 -0400)
committerDavid Zeuthen <davidz@redhat.com>
Wed, 30 Jun 2010 20:39:32 +0000 (16:39 -0400)
These fixes makes udisks-daemon from udisks' gdbus-port branch, see

 http://cgit.freedesktop.org/udisks/log/?h=gdbus-port

handle 200 add/remove uevents generated by e.g.

 #!/bin/bash
 DEV=mmcblk0p1
 for n in `seq 200` ; do
     udevadm trigger --sysname-match=$DEV --action=remove
     udevadm trigger --sysname-match=$DEV --action=add
     echo foo $n
 done

without any substantial leaks.

Signed-off-by: David Zeuthen <davidz@redhat.com>
gio/gdbusauth.c
gio/gdbusmessage.c
gio/gdbusprivate.c

index eeced636fb707038ba761c2da1e0d0c9940d78e9..d3a754e4591052a5fe8201fdc8c6d96f6f7d3646 100644 (file)
@@ -607,6 +607,8 @@ _g_dbus_auth_run_client (GDBusAuth     *auth,
 
   dis = G_DATA_INPUT_STREAM (g_data_input_stream_new (g_io_stream_get_input_stream (auth->priv->stream)));
   dos = G_DATA_OUTPUT_STREAM (g_data_output_stream_new (g_io_stream_get_output_stream (auth->priv->stream)));
+  g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (dis), FALSE);
+  g_filter_output_stream_set_close_base_stream (G_FILTER_OUTPUT_STREAM (dos), FALSE);
 
   g_data_input_stream_set_newline_type (dis, G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
 
@@ -863,8 +865,8 @@ _g_dbus_auth_run_client (GDBusAuth     *auth,
     g_object_unref (mech);
   g_ptr_array_unref (attempted_auth_mechs);
   g_strfreev (supported_auth_mechs);
-  g_object_ref (dis);
-  g_object_ref (dos);
+  g_object_unref (dis);
+  g_object_unref (dos);
 
   /* ensure return value is NULL if error is set */
   if (error != NULL && *error != NULL)
@@ -972,6 +974,8 @@ _g_dbus_auth_run_server (GDBusAuth              *auth,
 
   dis = G_DATA_INPUT_STREAM (g_data_input_stream_new (g_io_stream_get_input_stream (auth->priv->stream)));
   dos = G_DATA_OUTPUT_STREAM (g_data_output_stream_new (g_io_stream_get_output_stream (auth->priv->stream)));
+  g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (dis), FALSE);
+  g_filter_output_stream_set_close_base_stream (G_FILTER_OUTPUT_STREAM (dos), FALSE);
 
   g_data_input_stream_set_newline_type (dis, G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
 
@@ -1324,9 +1328,9 @@ _g_dbus_auth_run_server (GDBusAuth              *auth,
   if (mech != NULL)
     g_object_unref (mech);
   if (dis != NULL)
-    g_object_ref (dis);
-  if (dis != NULL)
-    g_object_ref (dos);
+    g_object_unref (dis);
+  if (dos != NULL)
+    g_object_unref (dos);
 
   /* ensure return value is FALSE if error is set */
   if (error != NULL && *error != NULL)
index 337ac754eaf2a6ebd5ba5039e3f6e9b59b7f6bba..ba0c7a91c9bf21fdefc7e7c2022e4df38b1faea2 100644 (file)
@@ -763,6 +763,8 @@ read_string (GMemoryInputStream    *mis,
 }
 
 /* if just_align==TRUE, don't read a value, just align the input stream wrt padding */
+
+/* returns a non-floating GVariant! */
 static GVariant *
 parse_value_from_blob (GMemoryInputStream    *mis,
                        GDataInputStream      *dis,
@@ -1053,6 +1055,7 @@ parse_value_from_blob (GMemoryInputStream    *mis,
                   goto fail;
                 }
               g_variant_builder_add_value (&builder, item);
+              g_variant_unref (item);
               offset = g_seekable_tell (G_SEEKABLE (mis));
             }
         }
@@ -1092,7 +1095,6 @@ parse_value_from_blob (GMemoryInputStream    *mis,
                                        &local_error);
           if (key == NULL)
             goto fail;
-
           value_type = g_variant_type_value (type);
           value = parse_value_from_blob (mis,
                                          dis,
@@ -1106,6 +1108,8 @@ parse_value_from_blob (GMemoryInputStream    *mis,
               goto fail;
             }
           ret = g_variant_new_dict_entry (key, value);
+          g_variant_unref (key);
+          g_variant_unref (value);
         }
     }
   else if (g_variant_type_is_tuple (type))
@@ -1140,6 +1144,7 @@ parse_value_from_blob (GMemoryInputStream    *mis,
                   goto fail;
                 }
               g_variant_builder_add_value (&builder, item);
+              g_variant_unref (item);
 
               element_type = g_variant_type_next (element_type);
             }
@@ -1188,6 +1193,7 @@ parse_value_from_blob (GMemoryInputStream    *mis,
           if (value == NULL)
             goto fail;
           ret = g_variant_new_variant (value);
+          g_variant_unref (value);
         }
     }
   else
@@ -1225,6 +1231,12 @@ parse_value_from_blob (GMemoryInputStream    *mis,
     }
 #endif /* DEBUG_SERIALIZER */
 
+  /* sink the reference */
+  if (ret != NULL)
+    {
+      g_assert (g_variant_is_floating (ret));
+      g_variant_ref_sink (ret);
+    }
   return ret;
 
  fail:
@@ -1413,9 +1425,8 @@ g_dbus_message_new_from_blob (guchar                *blob,
                                    error);
   if (headers == NULL)
     goto out;
-  g_variant_ref_sink (headers);
   g_variant_iter_init (&iter, headers);
-  while ((item = g_variant_iter_next_value (&iter)))
+  while ((item = g_variant_iter_next_value (&iter)) != NULL)
     {
       guchar header_field;
       GVariant *value;
@@ -1424,6 +1435,8 @@ g_dbus_message_new_from_blob (guchar                *blob,
                      &header_field,
                      &value);
       g_dbus_message_set_header (message, header_field, value);
+      g_variant_unref (value);
+      g_variant_unref (item);
     }
   g_variant_unref (headers);
 
@@ -1471,13 +1484,9 @@ g_dbus_message_new_from_blob (guchar                *blob,
                                                        FALSE,
                                                        2,
                                                        error);
-          if (message->priv->body == NULL)
-            {
-              g_variant_type_free (variant_type);
-              goto out;
-            }
-          g_variant_ref_sink (message->priv->body);
           g_variant_type_free (variant_type);
+          if (message->priv->body == NULL)
+            goto out;
         }
     }
   else
@@ -1716,7 +1725,7 @@ append_value_to_blob (GVariant             *value,
               guint n;
               n = 0;
               g_variant_iter_init (&iter, value);
-              while ((item = g_variant_iter_next_value (&iter)))
+              while ((item = g_variant_iter_next_value (&iter)) != NULL)
                 {
                   gsize padding_added_for_item;
                   if (!append_value_to_blob (item,
@@ -1725,7 +1734,11 @@ append_value_to_blob (GVariant             *value,
                                              dos,
                                              &padding_added_for_item,
                                              error))
-                    goto fail;
+                    {
+                      g_variant_unref (item);
+                      goto fail;
+                    }
+                  g_variant_unref (item);
                   if (n == 0)
                     {
                       array_payload_begin_offset += padding_added_for_item;
@@ -1755,7 +1768,7 @@ append_value_to_blob (GVariant             *value,
           GVariant *item;
           GVariantIter iter;
           g_variant_iter_init (&iter, value);
-          while ((item = g_variant_iter_next_value (&iter)))
+          while ((item = g_variant_iter_next_value (&iter)) != NULL)
             {
               if (!append_value_to_blob (item,
                                          g_variant_get_type (item),
@@ -1763,7 +1776,11 @@ append_value_to_blob (GVariant             *value,
                                          dos,
                                          NULL,
                                          error))
-                goto fail;
+                {
+                  g_variant_unref (item);
+                  goto fail;
+                }
+              g_variant_unref (item);
             }
         }
     }
@@ -1833,7 +1850,7 @@ append_body_to_blob (GVariant             *value,
     }
 
   g_variant_iter_init (&iter, value);
-  while ((item = g_variant_iter_next_value (&iter)))
+  while ((item = g_variant_iter_next_value (&iter)) != NULL)
     {
       if (!append_value_to_blob (item,
                                  g_variant_get_type (item),
@@ -1841,7 +1858,11 @@ append_body_to_blob (GVariant             *value,
                                  dos,
                                  NULL,
                                  error))
-        goto fail;
+        {
+          g_variant_unref (item);
+          goto fail;
+        }
+      g_variant_unref (item);
     }
   return TRUE;
 
@@ -2454,6 +2475,7 @@ g_dbus_message_get_arg0 (GDBusMessage  *message)
       item = g_variant_get_child_value (message->priv->body, 0);
       if (g_variant_is_of_type (item, G_VARIANT_TYPE_STRING))
         ret = g_variant_get_string (item, NULL);
+      g_variant_unref (item);
     }
 
   return ret;
index 5208ad993a547a19a4a8fbf957c5f807e994e8a6..30705bf72c7e7cabc7e0acb5380b57fdb707d8e9 100644 (file)
@@ -119,6 +119,7 @@ read_with_control_data_free (ReadWithControlData *data)
   g_object_unref (data->socket);
   if (data->cancellable != NULL)
     g_object_unref (data->cancellable);
+  g_object_unref (data->simple);
   g_free (data);
 }