[gnome-control-center/wip/rancell/info-g-auto] info: Use g_auto for variables



commit 3ea0355e857b3f223b0e0be61fc2794c70f43aa2
Author: Robert Ancell <robert ancell canonical com>
Date:   Mon Sep 25 16:52:58 2017 -0400

    info: Use g_auto for variables

 panels/info/cc-info-default-apps-panel.c    |   12 +--
 panels/info/cc-info-overview-panel.c        |  199 +++++++++++----------------
 panels/info/cc-info-panel.c                 |    6 +-
 panels/info/cc-info-removable-media-panel.c |   46 ++-----
 panels/info/gsd-disk-space-helper.c         |    8 +-
 panels/info/info-cleanup.c                  |   33 ++---
 panels/info/test-info-cleanup.c             |   17 +--
 7 files changed, 116 insertions(+), 205 deletions(-)
---
diff --git a/panels/info/cc-info-default-apps-panel.c b/panels/info/cc-info-default-apps-panel.c
index 1aa7404..b706476 100644
--- a/panels/info/cc-info-default-apps-panel.c
+++ b/panels/info/cc-info-default-apps-panel.c
@@ -78,8 +78,8 @@ static void
 default_app_changed (GtkAppChooserButton    *button,
                      CcInfoDefaultAppsPanel *self)
 {
-  GAppInfo *info;
-  GError *error = NULL;
+  g_autoptr(GAppInfo) info = NULL;
+  g_autoptr(GError) error = NULL;
   DefaultAppData *app_data;
   int i;
 
@@ -90,7 +90,6 @@ default_app_changed (GtkAppChooserButton    *button,
     {
       g_warning ("Failed to set '%s' as the default application for '%s': %s",
                  g_app_info_get_name (info), app_data->content_type, error->message);
-      g_error_free (error);
       error = NULL;
     }
   else
@@ -102,7 +101,7 @@ default_app_changed (GtkAppChooserButton    *button,
   if (app_data->extra_type_filter)
     {
       const char *const *mime_types;
-      GPatternSpec *pattern;
+      g_autoptr(GPatternSpec) pattern = NULL;
 
       pattern = g_pattern_spec_new (app_data->extra_type_filter);
       mime_types = g_app_info_get_supported_types (info);
@@ -117,7 +116,6 @@ default_app_changed (GtkAppChooserButton    *button,
               g_warning ("Failed to set '%s' as the default application for secondary "
                          "content type '%s': %s",
                          g_app_info_get_name (info), mime_types[i], error->message);
-              g_error_free (error);
             }
           else
             {
@@ -125,11 +123,7 @@ default_app_changed (GtkAppChooserButton    *button,
               g_app_info_get_name (info), mime_types[i]);
             }
         }
-
-      g_pattern_spec_free (pattern);
     }
-
-  g_object_unref (info);
 }
 
 #define OFFSET(x)             (G_STRUCT_OFFSET (CcInfoDefaultAppsPanel, x))
diff --git a/panels/info/cc-info-overview-panel.c b/panels/info/cc-info-overview-panel.c
index 3af58be..85ed0a3 100644
--- a/panels/info/cc-info-overview-panel.c
+++ b/panels/info/cc-info-overview-panel.c
@@ -105,6 +105,18 @@ typedef struct
   char **current;
 } VersionData;
 
+static void
+version_data_free (VersionData *data)
+{
+  g_free (data->major);
+  g_free (data->minor);
+  g_free (data->micro);
+  g_free (data->distributor);
+  g_free (data->date);
+  g_free (data);
+}
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC (VersionData, version_data_free);
 
 G_DEFINE_TYPE_WITH_PRIVATE (CcInfoOverviewPanel, cc_info_overview_panel, CC_TYPE_PANEL)
 
@@ -150,7 +162,13 @@ version_text_handler (GMarkupParseContext *ctx,
 {
   VersionData *data = user_data;
   if (data->current != NULL)
-    *data->current = g_strstrip (g_strdup (text));
+    {
+      g_autofree char *stripped = NULL;
+
+      stripped = g_strstrip (g_strdup (text));
+      g_free (*data->current);
+      *data->current = g_strdup (stripped);
+    }
 }
 
 static gboolean
@@ -165,14 +183,11 @@ load_gnome_version (char **version,
     NULL,
     NULL,
   };
-  GError              *error;
-  GMarkupParseContext *ctx;
-  char                *contents;
-  gsize                length;
-  VersionData         *data;
-  gboolean             ret;
-
-  ret = FALSE;
+  g_autoptr(GError) error = NULL;
+  g_autoptr(GMarkupParseContext) ctx = NULL;
+  g_autofree char *contents = NULL;
+  gsize length;
+  g_autoptr(VersionData) data = NULL;
 
   error = NULL;
   if (!g_file_get_contents (DATADIR "/gnome/gnome-version.xml",
@@ -197,19 +212,10 @@ load_gnome_version (char **version,
       if (date != NULL)
         *date = g_strdup (data->date);
 
-      ret = TRUE;
+      return TRUE;
     }
 
-  g_markup_parse_context_free (ctx);
-  g_free (data->major);
-  g_free (data->minor);
-  g_free (data->micro);
-  g_free (data->distributor);
-  g_free (data->date);
-  g_free (data);
-  g_free (contents);
-
-  return ret;
+  return FALSE;
 };
 
 static void
@@ -222,10 +228,10 @@ graphics_data_free (GraphicsData *gdata)
 static char *
 get_renderer_from_session (void)
 {
-  GDBusProxy *session_proxy;
-  GVariant *renderer_variant;
+  g_autoptr(GDBusProxy) session_proxy = NULL;
+  g_autoptr(GVariant) renderer_variant = NULL;
   char *renderer;
-  GError *error = NULL;
+  g_autoptr(GError) error = NULL;
 
   session_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
                                                  G_DBUS_PROXY_FLAGS_NONE,
@@ -238,12 +244,10 @@ get_renderer_from_session (void)
     {
       g_warning ("Unable to connect to create a proxy for org.gnome.SessionManager: %s",
                  error->message);
-      g_error_free (error);
       return NULL;
     }
 
   renderer_variant = g_dbus_proxy_get_cached_property (session_proxy, "Renderer");
-  g_object_unref (session_proxy);
 
   if (!renderer_variant)
     {
@@ -252,7 +256,6 @@ get_renderer_from_session (void)
     }
 
   renderer = info_cleanup (g_variant_get_string (renderer_variant, NULL));
-  g_variant_unref (renderer_variant);
 
   return renderer;
 }
@@ -262,10 +265,9 @@ get_renderer_from_helper (gboolean discrete_gpu)
 {
   int status;
   char *argv[] = { GNOME_SESSION_DIR "/gnome-session-check-accelerated", NULL };
-  char **envp = NULL;
-  char *renderer = NULL;
-  char *ret = NULL;
-  GError *error = NULL;
+  g_auto(GStrv) envp = NULL;
+  g_autofree char *renderer = NULL;
+  g_autoptr(GError) error = NULL;
 
   if (discrete_gpu)
     {
@@ -278,31 +280,25 @@ get_renderer_from_helper (gboolean discrete_gpu)
       g_debug ("Failed to get %s GPU: %s",
                discrete_gpu ? "discrete" : "integrated",
                error->message);
-      g_error_free (error);
-      goto out;
+      return NULL;
     }
 
   if (!g_spawn_check_exit_status (status, NULL))
-    goto out;
+    return NULL;
 
   if (renderer == NULL || *renderer == '\0')
-    goto out;
-
-  ret = info_cleanup (renderer);
+    return NULL;
 
-out:
-  g_free (renderer);
-  g_strfreev (envp);
-  return ret;
+  return info_cleanup (renderer);
 }
 
 static gboolean
 has_dual_gpu (void)
 {
-  GDBusProxy *switcheroo_proxy;
-  GVariant *dualgpu_variant;
+  g_autoptr(GDBusProxy) switcheroo_proxy = NULL;
+  g_autoptr(GVariant) dualgpu_variant = NULL;
   gboolean ret;
-  GError *error = NULL;
+  g_autoptr(GError) error = NULL;
 
   switcheroo_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
                                                     G_DBUS_PROXY_FLAGS_NONE,
@@ -315,12 +311,10 @@ has_dual_gpu (void)
     {
       g_debug ("Unable to connect to create a proxy for net.hadess.SwitcherooControl: %s",
                error->message);
-      g_error_free (error);
       return FALSE;
     }
 
   dualgpu_variant = g_dbus_proxy_get_cached_property (switcheroo_proxy, "HasDualGpu");
-  g_object_unref (switcheroo_proxy);
 
   if (!dualgpu_variant)
     {
@@ -329,7 +323,6 @@ has_dual_gpu (void)
     }
 
   ret = g_variant_get_boolean (dualgpu_variant);
-  g_variant_unref (dualgpu_variant);
 
   if (ret)
     g_debug ("Dual-GPU machine detected");
@@ -358,8 +351,8 @@ get_graphics_data (void)
 
   if (x11_or_wayland)
     {
-      char *discrete_renderer = NULL;
-      char *renderer;
+      g_autofree char *discrete_renderer = NULL;
+      g_autofree char *renderer = NULL;
 
       renderer = get_renderer_from_session ();
       if (!renderer)
@@ -372,8 +365,6 @@ get_graphics_data (void)
         result->hardware_string = g_strdup_printf ("%s / %s",
                                                    renderer,
                                                    discrete_renderer);
-      g_free (renderer);
-      g_free (discrete_renderer);
     }
 #endif
 
@@ -387,32 +378,31 @@ static GHashTable*
 get_os_info (void)
 {
   GHashTable *hashtable;
-  gchar *buffer;
+  g_autofree gchar *buffer = NULL;
 
   hashtable = NULL;
 
   if (g_file_get_contents ("/etc/os-release", &buffer, NULL, NULL))
     {
-      gchar **lines;
+      g_auto(GStrv) lines = NULL;
       gint i;
 
       lines = g_strsplit (buffer, "\n", -1);
 
       for (i = 0; lines[i] != NULL; i++)
         {
-          gchar *delimiter, *key, *value;
+          gchar *delimiter;
 
           /* Initialize the hash table if needed */
           if (!hashtable)
             hashtable = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
 
           delimiter = strstr (lines[i], "=");
-          value = NULL;
-          key = NULL;
 
           if (delimiter != NULL)
             {
               gint size;
+              gchar *key, *value;
 
               key = g_strndup (lines[i], delimiter - lines[i]);
 
@@ -434,9 +424,6 @@ get_os_info (void)
               g_hash_table_insert (hashtable, key, value);
             }
         }
-
-      g_strfreev (lines);
-      g_free (buffer);
     }
 
   return hashtable;
@@ -502,30 +489,24 @@ query_done (GFile               *file,
             CcInfoOverviewPanel *self)
 {
   CcInfoOverviewPanelPrivate *priv;
-  GFileInfo *info;
-  GError *error = NULL;
+  g_autoptr(GFileInfo) info = NULL;
+  g_autoptr(GError) error = NULL;
 
   info = g_file_query_filesystem_info_finish (file, res, &error);
   if (info != NULL)
     {
       priv = cc_info_overview_panel_get_instance_private (self);
       priv->total_bytes += g_file_info_get_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE);
-      g_object_unref (info);
     }
   else
     {
       if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
-        {
-          g_error_free (error);
           return;
-        }
       else
         {
-          char *path;
+          g_autofree char *path = NULL;
           path = g_file_get_path (file);
           g_warning ("Failed to get filesystem free space for '%s': %s", path, error->message);
-          g_free (path);
-          g_error_free (error);
         }
     }
 
@@ -537,16 +518,15 @@ static void
 get_primary_disc_info_start (CcInfoOverviewPanel *self)
 {
   GUnixMountEntry *mount;
-  GFile *file;
+  g_autoptr(GFile) file = NULL;
   CcInfoOverviewPanelPrivate *priv = cc_info_overview_panel_get_instance_private (self);
 
   if (priv->primary_mounts == NULL)
     {
-      char *size;
+      g_autofree char *size = NULL;
 
       size = g_format_size (priv->total_bytes);
       gtk_label_set_text (GTK_LABEL (priv->disk_label), size);
-      g_free (size);
 
       return;
     }
@@ -562,7 +542,6 @@ get_primary_disc_info_start (CcInfoOverviewPanel *self)
                                       priv->cancellable,
                                       (GAsyncReadyCallback) query_done,
                                       self);
-  g_object_unref (file);
 }
 
 static void
@@ -615,9 +594,8 @@ get_primary_disc_info (CcInfoOverviewPanel *self)
 static char *
 get_cpu_info (const glibtop_sysinfo *info)
 {
-  GHashTable    *counts;
-  GString       *cpu;
-  char          *ret;
+  g_autoptr(GHashTable) counts = NULL;
+  g_autoptr(GString) cpu = NULL;
   GHashTableIter iter;
   gpointer       key, value;
   int            i;
@@ -654,8 +632,8 @@ get_cpu_info (const glibtop_sysinfo *info)
   g_hash_table_iter_init (&iter, counts);
   while (g_hash_table_iter_next (&iter, &key, &value))
     {
-      char *cleanedup;
-      int   count;
+      g_autofree char *cleanedup = NULL;
+      int count;
 
       count = GPOINTER_TO_INT (value);
       cleanedup = info_cleanup ((const char *) key);
@@ -663,14 +641,9 @@ get_cpu_info (const glibtop_sysinfo *info)
         g_string_append_printf (cpu, "%s \303\227 %d ", cleanedup, count);
       else
         g_string_append_printf (cpu, "%s ", cleanedup);
-      g_free (cleanedup);
     }
 
-  g_hash_table_destroy (counts);
-
-  ret = g_string_free (cpu, FALSE);
-
-  return ret;
+  return g_strdup (cpu->str);
 }
 
 static void
@@ -740,13 +713,10 @@ set_virtualization_label (CcInfoOverviewPanel *self,
 static void
 info_overview_panel_setup_virt (CcInfoOverviewPanel *self)
 {
-  GError *error = NULL;
-  GDBusProxy *systemd_proxy;
-  GVariant *variant;
+  g_autoptr(GError) error = NULL;
+  g_autoptr(GDBusProxy) systemd_proxy = NULL;
+  g_autoptr(GVariant) variant = NULL;
   GVariant *inner;
-  char *str;
-
-  str = NULL;
 
   systemd_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
                                                  G_DBUS_PROXY_FLAGS_NONE,
@@ -760,8 +730,8 @@ info_overview_panel_setup_virt (CcInfoOverviewPanel *self)
   if (systemd_proxy == NULL)
     {
       g_debug ("systemd not available, bailing: %s", error->message);
-      g_error_free (error);
-      goto bail;
+      set_virtualization_label (self, NULL);
+      return;
     }
 
   variant = g_dbus_proxy_call_sync (systemd_proxy,
@@ -774,20 +744,12 @@ info_overview_panel_setup_virt (CcInfoOverviewPanel *self)
   if (variant == NULL)
     {
       g_debug ("Failed to get property '%s': %s", "Virtualization", error->message);
-      g_error_free (error);
-      g_object_unref (systemd_proxy);
-      goto bail;
+      set_virtualization_label (self, NULL);
+      return;
     }
 
   g_variant_get (variant, "(v)", &inner);
-  str = g_variant_dup_string (inner, NULL);
-  g_variant_unref (variant);
-
-  g_object_unref (systemd_proxy);
-
-bail:
-  set_virtualization_label (self, str);
-  g_free (str);
+  set_virtualization_label (self, g_variant_get_string (inner, NULL));
 }
 
 static void
@@ -796,7 +758,10 @@ info_overview_panel_setup_overview (CcInfoOverviewPanel *self)
   gboolean    res;
   glibtop_mem mem;
   const glibtop_sysinfo *info;
-  char       *text;
+  g_autofree char *memory_text = NULL;
+  g_autofree char *cpu_text = NULL;
+  g_autofree char *os_type_text = NULL;
+  g_autofree char *os_name_text = NULL;
   CcInfoOverviewPanelPrivate *priv = cc_info_overview_panel_get_instance_private (self);
 
   res = load_gnome_version (&priv->gnome_version,
@@ -804,29 +769,25 @@ info_overview_panel_setup_overview (CcInfoOverviewPanel *self)
                             &priv->gnome_date);
   if (res)
     {
+      g_autofree gchar *text = NULL;
       text = g_strdup_printf (_("Version %s"), priv->gnome_version);
       gtk_label_set_text (GTK_LABEL (priv->version_label), text);
-      g_free (text);
     }
 
   glibtop_get_mem (&mem);
-  text = g_format_size_full (mem.total, G_FORMAT_SIZE_IEC_UNITS);
-  gtk_label_set_text (GTK_LABEL (priv->memory_label), text ? text : "");
-  g_free (text);
+  memory_text = g_format_size_full (mem.total, G_FORMAT_SIZE_IEC_UNITS);
+  gtk_label_set_text (GTK_LABEL (priv->memory_label), memory_text ? memory_text : "");
 
   info = glibtop_get_sysinfo ();
 
-  text = get_cpu_info (info);
-  gtk_label_set_markup (GTK_LABEL (priv->processor_label), text ? text : "");
-  g_free (text);
+  cpu_text = get_cpu_info (info);
+  gtk_label_set_markup (GTK_LABEL (priv->processor_label), cpu_text ? cpu_text : "");
 
-  text = get_os_type ();
-  gtk_label_set_text (GTK_LABEL (priv->os_type_label), text ? text : "");
-  g_free (text);
+  os_type_text = get_os_type ();
+  gtk_label_set_text (GTK_LABEL (priv->os_type_label), os_type_text ? os_type_text : "");
 
-  text = get_os_name ();
-  gtk_label_set_text (GTK_LABEL (priv->os_name_label), text ? text : "");
-  g_free (text);
+  os_name_text = get_os_name ();
+  gtk_label_set_text (GTK_LABEL (priv->os_name_label), os_name_text ? os_name_text : "");
 
   get_primary_disc_info (self);
 
@@ -849,9 +810,9 @@ static void
 on_updates_button_clicked (GtkWidget           *widget,
                            CcInfoOverviewPanel *self)
 {
-  GError *error = NULL;
+  g_autoptr(GError) error = NULL;
   gboolean ret;
-  gchar **argv;
+  g_auto(GStrv) argv = NULL;
 
   argv = g_new0 (gchar *, 3);
   if (does_gnome_software_exist ())
@@ -865,11 +826,7 @@ on_updates_button_clicked (GtkWidget           *widget,
     }
   ret = g_spawn_async (NULL, argv, NULL, 0, NULL, NULL, NULL, &error);
   if (!ret)
-    {
       g_warning ("Failed to spawn %s: %s", argv[0], error->message);
-      g_error_free (error);
-    }
-  g_strfreev (argv);
 }
 
 static void
diff --git a/panels/info/cc-info-panel.c b/panels/info/cc-info-panel.c
index 1addd16..052c370 100644
--- a/panels/info/cc-info-panel.c
+++ b/panels/info/cc-info-panel.c
@@ -112,7 +112,7 @@ static void
 info_panel_setup_selector (CcInfoPanel  *self)
 {
   GtkTreeView *view;
-  GtkListStore *model;
+  g_autoptr(GtkListStore) model = NULL;
   GtkTreeSelection *selection;
   GtkTreeViewColumn *column;
   GtkCellRenderer *renderer;
@@ -124,7 +124,6 @@ info_panel_setup_selector (CcInfoPanel  *self)
 
   model = gtk_list_store_new (1, G_TYPE_STRING);
   gtk_tree_view_set_model (view, GTK_TREE_MODEL (model));
-  g_object_unref (model);
 
   renderer = gtk_cell_renderer_text_new ();
   gtk_cell_renderer_set_padding (renderer, 4, 4);
@@ -174,7 +173,7 @@ info_panel_setup_overview (CcInfoPanel  *self)
 static void
 cc_info_panel_init (CcInfoPanel *self)
 {
-  GError *error = NULL;
+  g_autoptr(GError) error = NULL;
 
   g_resources_register (cc_info_get_resource ());
 
@@ -185,7 +184,6 @@ cc_info_panel_init (CcInfoPanel *self)
                                      &error) == 0)
     {
       g_warning ("Could not load interface file: %s", error->message);
-      g_error_free (error);
       return;
     }
 
diff --git a/panels/info/cc-info-removable-media-panel.c b/panels/info/cc-info-removable-media-panel.c
index f6a3b52..facc70b 100644
--- a/panels/info/cc-info-removable-media-panel.c
+++ b/panels/info/cc-info-removable-media-panel.c
@@ -156,9 +156,9 @@ autorun_get_preferences (CcInfoRemovableMediaPanel *self,
                          gboolean                  *pref_ignore,
                          gboolean                  *pref_open_folder)
 {
-  char **x_content_start_app;
-  char **x_content_ignore;
-  char **x_content_open_folder;
+  g_auto(GStrv) x_content_start_app = NULL;
+  g_auto(GStrv) x_content_ignore = NULL;
+  g_auto(GStrv) x_content_open_folder = NULL;
 
   g_return_if_fail (pref_start_app != NULL);
   g_return_if_fail (pref_ignore != NULL);
@@ -182,9 +182,6 @@ autorun_get_preferences (CcInfoRemovableMediaPanel *self,
   if (x_content_open_folder != NULL) {
     *pref_open_folder = media_panel_g_strv_find (x_content_open_folder, x_content_type) != -1;
   }
-  g_strfreev (x_content_ignore);
-  g_strfreev (x_content_start_app);
-  g_strfreev (x_content_open_folder);
 }
 
 static void
@@ -194,9 +191,9 @@ autorun_set_preferences (CcInfoRemovableMediaPanel *self,
                          gboolean                   pref_ignore,
                          gboolean                   pref_open_folder)
 {
-  char **x_content_start_app;
-  char **x_content_ignore;
-  char **x_content_open_folder;
+  g_auto(GStrv) x_content_start_app = NULL;
+  g_auto(GStrv) x_content_ignore = NULL;
+  g_auto(GStrv) x_content_open_folder = NULL;
 
   g_assert (x_content_type != NULL);
 
@@ -228,10 +225,6 @@ autorun_set_preferences (CcInfoRemovableMediaPanel *self,
   g_settings_set_strv (self->media_settings,
                        PREF_MEDIA_AUTORUN_X_CONTENT_OPEN_FOLDER, (const gchar * const*) 
x_content_open_folder);
 
-  g_strfreev (x_content_open_folder);
-  g_strfreev (x_content_ignore);
-  g_strfreev (x_content_start_app);
-
 }
 
 static void
@@ -240,7 +233,7 @@ custom_item_activated_cb (GtkAppChooserButton *button,
                           gpointer             user_data)
 {
   CcInfoRemovableMediaPanel *self = user_data;
-  gchar *content_type;
+  g_autofree gchar *content_type = NULL;
 
   content_type = gtk_app_chooser_get_content_type (GTK_APP_CHOOSER (button));
 
@@ -254,8 +247,6 @@ custom_item_activated_cb (GtkAppChooserButton *button,
     autorun_set_preferences (self, content_type,
                              FALSE, TRUE, FALSE);
   }
-
-  g_free (content_type);
 }
 
 static void
@@ -263,8 +254,8 @@ combo_box_changed_cb (GtkComboBox *combo_box,
                       gpointer     user_data)
 {
   CcInfoRemovableMediaPanel *self = user_data;
-  GAppInfo *info;
-  gchar *content_type;
+  g_autoptr(GAppInfo) info = NULL;
+  g_autofree gchar *content_type = NULL;
 
   info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (combo_box));
 
@@ -275,9 +266,6 @@ combo_box_changed_cb (GtkComboBox *combo_box,
   autorun_set_preferences (self, content_type,
                            TRUE, FALSE, FALSE);
   g_app_info_set_as_default_for_type (info, content_type, NULL);
-
-  g_object_unref (info);
-  g_free (content_type);
 }
 
 static void
@@ -290,8 +278,8 @@ prepare_combo_box (CcInfoRemovableMediaPanel *self,
   gboolean pref_start_app;
   gboolean pref_ignore;
   gboolean pref_open_folder;
-  GAppInfo *info;
-  gchar *content_type;
+  g_autoptr(GAppInfo) info = NULL;
+  g_autofree gchar *content_type = NULL;
 
   content_type = gtk_app_chooser_get_content_type (GTK_APP_CHOOSER (app_chooser));
 
@@ -305,7 +293,6 @@ prepare_combo_box (CcInfoRemovableMediaPanel *self,
   /* append the separator only if we have >= 1 apps in the chooser */
   if (info != NULL) {
     gtk_app_chooser_button_append_separator (app_chooser);
-    g_object_unref (info);
   }
 
   gtk_app_chooser_button_append_custom_item (app_chooser, CUSTOM_ITEM_ASK,
@@ -337,8 +324,6 @@ prepare_combo_box (CcInfoRemovableMediaPanel *self,
                     G_CALLBACK (combo_box_changed_cb), self);
   g_signal_connect (app_chooser, "custom-item-activated",
                     G_CALLBACK (custom_item_activated_cb), self);
-
-  g_free (content_type);
 }
 
 static void
@@ -347,12 +332,10 @@ other_type_combo_box_changed (GtkComboBox               *combo_box,
 {
   GtkTreeIter iter;
   GtkTreeModel *model;
-  char *x_content_type;
+  g_autofree gchar *x_content_type = NULL;
   GtkWidget *action_container;
   GtkWidget *action_label;
 
-  x_content_type = NULL;
-
   if (!gtk_combo_box_get_active_iter (combo_box, &iter)) {
     return;
   }
@@ -380,8 +363,6 @@ other_type_combo_box_changed (GtkComboBox               *combo_box,
   action_label = self->media_other_action_label;
 
   gtk_label_set_mnemonic_widget (GTK_LABEL (action_label), self->other_application_combo);
-
-  g_free (x_content_type);
 }
 
 static void
@@ -491,7 +472,7 @@ info_panel_setup_media (CcInfoRemovableMediaPanel *self)
 
   for (l = content_types; l != NULL; l = l->next) {
     char *content_type = l->data;
-    char *description = NULL;
+    g_autofree char *description = NULL;
 
     if (!g_str_has_prefix (content_type, "x-content/"))
       continue;
@@ -525,7 +506,6 @@ info_panel_setup_media (CcInfoRemovableMediaPanel *self)
                         0, description,
                         1, content_type,
                         -1);
-    g_free (description);
   skip:
     ;
   }
diff --git a/panels/info/gsd-disk-space-helper.c b/panels/info/gsd-disk-space-helper.c
index ff1b227..3da31fb 100644
--- a/panels/info/gsd-disk-space-helper.c
+++ b/panels/info/gsd-disk-space-helper.c
@@ -109,17 +109,15 @@ gboolean
 gsd_is_removable_mount (GUnixMountEntry *mount)
 {
         const char *mount_path;
-        char *path;
+        g_autofree gchar *path = NULL;
 
         mount_path = g_unix_mount_get_mount_path (mount);
         if (mount_path == NULL)
                 return FALSE;
 
         path = g_strdup_printf ("/run/media/%s", g_get_user_name ());
-        if (g_str_has_prefix (mount_path, path)) {
-                g_free (path);
+        if (g_str_has_prefix (mount_path, path))
                 return TRUE;
-        }
-        g_free (path);
+
         return FALSE;
 }
diff --git a/panels/info/info-cleanup.c b/panels/info/info-cleanup.c
index bb47493..2fda273 100644
--- a/panels/info/info-cleanup.c
+++ b/panels/info/info-cleanup.c
@@ -32,6 +32,7 @@ typedef struct
 static char *
 prettify_info (const char *info)
 {
+  g_autofree char *escaped = NULL;
   char *pretty;
   int   i;
   static const ReplaceStrings rs[] = {
@@ -49,14 +50,14 @@ prettify_info (const char *info)
   if (*info == '\0')
     return NULL;
 
-  pretty = g_markup_escape_text (info, -1);
-  pretty = g_strchug (g_strchomp (pretty));
+  escaped = g_markup_escape_text (info, -1);
+  pretty = g_strdup (g_strstrip (escaped));
 
   for (i = 0; i < G_N_ELEMENTS (rs); i++)
     {
-      GError *error;
-      GRegex *re;
-      char   *new;
+      g_autoptr(GError) error = NULL;
+      g_autoptr(GRegex) re = NULL;
+      g_autofree char *new = NULL;
 
       error = NULL;
 
@@ -64,7 +65,6 @@ prettify_info (const char *info)
       if (re == NULL)
         {
           g_warning ("Error building regex: %s", error->message);
-          g_error_free (error);
           continue;
         }
 
@@ -76,17 +76,14 @@ prettify_info (const char *info)
                              0,
                              &error);
 
-      g_regex_unref (re);
-
       if (error != NULL)
         {
           g_warning ("Error replacing %s: %s", rs[i].regex, error->message);
-          g_error_free (error);
           continue;
         }
 
       g_free (pretty);
-      pretty = new;
+      pretty = g_steal_pointer (&new);
     }
 
   return pretty;
@@ -95,9 +92,9 @@ prettify_info (const char *info)
 static char *
 remove_duplicate_whitespace (const char *old)
 {
-  char   *new;
-  GRegex *re;
-  GError *error;
+  char *new;
+  g_autoptr(GRegex) re = NULL;
+  g_autoptr(GError) error = NULL;
 
   if (old == NULL)
     return NULL;
@@ -107,7 +104,6 @@ remove_duplicate_whitespace (const char *old)
   if (re == NULL)
     {
       g_warning ("Error building regex: %s", error->message);
-      g_error_free (error);
       return g_strdup (old);
     }
   new = g_regex_replace (re,
@@ -117,11 +113,9 @@ remove_duplicate_whitespace (const char *old)
                          " ",
                          0,
                          &error);
-  g_regex_unref (re);
   if (new == NULL)
     {
       g_warning ("Error replacing string: %s", error->message);
-      g_error_free (error);
       return g_strdup (old);
     }
 
@@ -131,11 +125,8 @@ remove_duplicate_whitespace (const char *old)
 char *
 info_cleanup (const char *input)
 {
-  char *pretty, *ret;
+  g_autofree char *pretty;
 
   pretty = prettify_info (input);
-  ret = remove_duplicate_whitespace (pretty);
-  g_free (pretty);
-
-  return ret;
+  return remove_duplicate_whitespace (pretty);
 }
diff --git a/panels/info/test-info-cleanup.c b/panels/info/test-info-cleanup.c
index 33ad91b..3e6bebd 100644
--- a/panels/info/test-info-cleanup.c
+++ b/panels/info/test-info-cleanup.c
@@ -27,10 +27,9 @@
 static void
 test_info (void)
 {
-       char *contents;
-       char *result;
+       g_autofree gchar *contents = NULL;
        guint i;
-       char **lines;
+       g_auto(GStrv) lines = NULL;
 
        if (g_file_get_contents (TEST_SRCDIR "/info-cleanup-test.txt", &contents, NULL, NULL) == FALSE) {
                g_warning ("Failed to load '%s'", TEST_SRCDIR "/info-cleanup-test.txt");
@@ -46,8 +45,9 @@ test_info (void)
        }
 
        for (i = 0; lines[i] != NULL; i++) {
-               char *utf8;
-               char **items;
+               g_auto(GStrv) items = NULL;
+               g_autofree gchar *utf8 = NULL;
+               g_autofree gchar *result = NULL;
 
                if (*lines[i] == '#')
                        continue;
@@ -65,14 +65,7 @@ test_info (void)
                        g_debug ("Result for '%s' matches '%s'",
                                 utf8, result);
                }
-               g_free (result);
-               g_free (utf8);
-
-               g_strfreev (items);
        }
-
-       g_strfreev (lines);
-       g_free (contents);
 }
 
 int main (int argc, char **argv)


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