[glib] GDBus: plug some memory leaks



commit 21d7ce97c38bafa1fdbedc19fe9634bc3333e7c6
Author: David Zeuthen <davidz redhat com>
Date:   Wed Jun 30 16:36:47 2010 -0400

    GDBus: plug some memory leaks
    
    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    |   14 +++++++++-----
 gio/gdbusmessage.c |   52 +++++++++++++++++++++++++++++++++++++---------------
 gio/gdbusprivate.c |    1 +
 3 files changed, 47 insertions(+), 20 deletions(-)
---
diff --git a/gio/gdbusauth.c b/gio/gdbusauth.c
index eeced63..d3a754e 100644
--- a/gio/gdbusauth.c
+++ b/gio/gdbusauth.c
@@ -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)
diff --git a/gio/gdbusmessage.c b/gio/gdbusmessage.c
index 337ac75..ba0c7a9 100644
--- a/gio/gdbusmessage.c
+++ b/gio/gdbusmessage.c
@@ -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;
diff --git a/gio/gdbusprivate.c b/gio/gdbusprivate.c
index 5208ad9..30705bf 100644
--- a/gio/gdbusprivate.c
+++ b/gio/gdbusprivate.c
@@ -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);
 }
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]