[gnome-control-center] datetime: Use g_auto for variables



commit d71f705d77bbea7031c723c167866c908a7b6aa1
Author: Robert Ancell <robert ancell canonical com>
Date:   Tue Sep 26 21:02:36 2017 -0400

    datetime: Use g_auto for variables
    
    https://bugzilla.gnome.org/show_bug.cgi?id=788221

 panels/datetime/cc-datetime-panel.c |  151 ++++++++++++-----------------------
 panels/datetime/cc-timezone-map.c   |   33 ++------
 panels/datetime/test-timezone-gfx.c |    9 +--
 panels/datetime/test-timezone.c     |   14 +--
 panels/datetime/tz.c                |   38 +++------
 panels/datetime/tz.h                |    4 +
 6 files changed, 83 insertions(+), 166 deletions(-)
---
diff --git a/panels/datetime/cc-datetime-panel.c b/panels/datetime/cc-datetime-panel.c
index 8d98721..3063224 100644
--- a/panels/datetime/cc-datetime-panel.c
+++ b/panels/datetime/cc-datetime-panel.c
@@ -266,7 +266,7 @@ update_time (CcDateTimePanel *self)
   GtkWidget *h_spinbutton;
   GtkWidget *m_spinbutton;
   GtkWidget *am_pm_button;
-  char *label;
+  g_autofree gchar *label = NULL;
   gint hour;
   gint minute;
   gboolean use_ampm;
@@ -336,7 +336,6 @@ update_time (CcDateTimePanel *self)
     }
 
   gtk_label_set_text (GTK_LABEL (W ("datetime_label")), label);
-  g_free (label);
 }
 
 static void
@@ -345,16 +344,14 @@ set_time_cb (GObject      *source,
              gpointer      user_data)
 {
   CcDateTimePanel *self = user_data;
-  GError *error;
+  g_autoptr(GError) error = NULL;
 
-  error = NULL;
   if (!timedate1_call_set_time_finish (self->dtm,
                                        res,
                                        &error))
     {
       /* TODO: display any error in a user friendly way */
       g_warning ("Could not set system time: %s", error->message);
-      g_error_free (error);
     }
   else
     {
@@ -368,16 +365,14 @@ set_timezone_cb (GObject      *source,
                  gpointer      user_data)
 {
   CcDateTimePanel *self = user_data;
-  GError *error;
+  g_autoptr(GError) error = NULL;
 
-  error = NULL;
   if (!timedate1_call_set_timezone_finish (self->dtm,
                                            res,
                                            &error))
     {
       /* TODO: display any error in a user friendly way */
       g_warning ("Could not set system timezone: %s", error->message);
-      g_error_free (error);
     }
 }
 
@@ -387,16 +382,14 @@ set_using_ntp_cb (GObject      *source,
                   gpointer      user_data)
 {
   CcDateTimePanel *self = user_data;
-  GError *error;
+  g_autoptr(GError) error = NULL;
 
-  error = NULL;
   if (!timedate1_call_set_ntp_finish (self->dtm,
                                       res,
                                       &error))
     {
       /* TODO: display any error in a user friendly way */
       g_warning ("Could not set system to use NTP: %s", error->message);
-      g_error_free (error);
     }
 }
 
@@ -451,19 +444,17 @@ static void
 change_date (CcDateTimePanel *self)
 {
   guint mon, y, d;
-  GDateTime *old_date;
-
-  old_date = self->date;
+  g_autoptr(GDateTime) old_date = NULL;
 
   mon = 1 + gtk_combo_box_get_active (GTK_COMBO_BOX (W ("month-combobox")));
   y = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (W ("year-spinbutton")));
   d = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (W ("day-spinbutton")));
 
+  old_date = self->date;
   self->date = g_date_time_new_local (y, mon, d,
                                       g_date_time_get_hour (old_date),
                                       g_date_time_get_minute (old_date),
                                       g_date_time_get_second (old_date));
-  g_date_time_unref (old_date);
   queue_set_datetime (self);
 }
 
@@ -474,12 +465,11 @@ city_changed_cb (GtkEntryCompletion *entry_completion,
                  CcDateTimePanel *self)
 {
   GtkWidget *entry;
-  gchar *zone;
+  g_autofree gchar *zone = NULL;
 
   gtk_tree_model_get (model, iter,
                       CITY_COL_ZONE, &zone, -1);
   cc_timezone_map_set_timezone (CC_TIMEZONE_MAP (self->map), zone);
-  g_free (zone);
 
   entry = gtk_entry_completion_get_entry (GTK_ENTRY_COMPLETION (entry_completion));
   gtk_entry_set_text (GTK_ENTRY (entry), "");
@@ -490,10 +480,10 @@ city_changed_cb (GtkEntryCompletion *entry_completion,
 static char *
 translated_city_name (TzLocation *loc)
 {
-  char *country;
-  char *name;
-  char *zone_translated;
-  char **split_translated;
+  g_autofree gchar *zone_translated = NULL;
+  g_auto(GStrv) split_translated = NULL;
+  g_autofree gchar *country = NULL;
+  gchar *name;
   gint length;
 
   /* Load the translation for it */
@@ -502,7 +492,6 @@ translated_city_name (TzLocation *loc)
   split_translated = g_regex_split_simple ("[\\x{2044}\\x{2215}\\x{29f8}\\x{ff0f}/]",
                                            zone_translated,
                                            0, 0);
-  g_free (zone_translated);
 
   length = g_strv_length (split_translated);
 
@@ -511,8 +500,6 @@ translated_city_name (TzLocation *loc)
   name = g_strdup_printf (C_("timezone loc", "%s, %s"),
                           split_translated[length-1],
                           country);
-  g_free (country);
-  g_strfreev (split_translated);
 
   return name;
 }
@@ -520,12 +507,12 @@ translated_city_name (TzLocation *loc)
 static void
 update_timezone (CcDateTimePanel *self)
 {
-  char *bubble_text;
-  char *city_country;
-  char *label;
-  char *time_label;
-  char *utc_label;
-  char *tz_desc;
+  g_autofree gchar *bubble_text = NULL;
+  g_autofree gchar *city_country = NULL;
+  g_autofree gchar *label = NULL;
+  g_autofree gchar *time_label = NULL;
+  g_autofree gchar *utc_label = NULL;
+  g_autofree gchar *tz_desc = NULL;
   gboolean use_ampm;
 
   if (self->clock_format == G_DESKTOP_CLOCK_FORMAT_12H)
@@ -541,7 +528,6 @@ update_timezone (CcDateTimePanel *self)
                            g_date_time_get_timezone_abbreviation (self->date),
                            city_country);
   gtk_label_set_text (GTK_LABEL (W ("timezone_label")), label);
-  g_free (label);
 
   /* Translators: UTC here means the Coordinated Universal Time.
    * %:::z will be replaced by the offset from UTC e.g. UTC+02 */
@@ -570,12 +556,6 @@ update_timezone (CcDateTimePanel *self)
                                  city_country,
                                  time_label);
   cc_timezone_map_set_bubble_text (CC_TIMEZONE_MAP (self->map), bubble_text);
-
-  g_free (tz_desc);
-  g_free (bubble_text);
-  g_free (city_country);
-  g_free (time_label);
-  g_free (utc_label);
 }
 
 static void
@@ -583,20 +563,16 @@ location_changed_cb (CcTimezoneMap   *map,
                      TzLocation      *location,
                      CcDateTimePanel *self)
 {
-  GDateTime *old_date;
-  GTimeZone *timezone;
+  g_autoptr(GDateTime) old_date = NULL;
+  g_autoptr(GTimeZone) timezone = NULL;
 
   g_debug ("location changed to %s/%s", location->country, location->zone);
 
   self->current_location = location;
 
-  old_date = self->date;
-
   timezone = g_time_zone_new (location->zone);
+  old_date = self->date;
   self->date = g_date_time_to_timezone (old_date, timezone);
-  g_time_zone_unref (timezone);
-
-  g_date_time_unref (old_date);
 
   update_timezone (self);
   queue_set_timezone (self);
@@ -623,24 +599,22 @@ static void
 load_cities (TzLocation   *loc,
              GtkListStore *city_store)
 {
-  char *human_readable;
+  g_autofree gchar *human_readable = NULL;
 
   human_readable = translated_city_name (loc);
   gtk_list_store_insert_with_values (city_store, NULL, 0,
                                      CITY_COL_CITY_HUMAN_READABLE, human_readable,
                                      CITY_COL_ZONE, loc->zone,
                                      -1);
-  g_free (human_readable);
 }
 
 static void
 load_regions_model (GtkListStore *cities)
 {
-  TzDB *db;
+  g_autoptr(TzDB) db = NULL;
 
   db = tz_load_db ();
   g_ptr_array_foreach (db->locations, (GFunc) load_cities, cities);
-  tz_db_free (db);
 }
 
 static void
@@ -690,9 +664,7 @@ static void
 change_time (CcDateTimePanel *self)
 {
   guint h, m;
-  GDateTime *old_date;
-
-  old_date = self->date;
+  g_autoptr(GDateTime) old_date = NULL;
 
   h = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (W ("h_spinbutton")));
   m = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (W ("m_spinbutton")));
@@ -714,12 +686,12 @@ change_time (CcDateTimePanel *self)
         h += 12;
     }
 
+  old_date = self->date;
   self->date = g_date_time_new_local (g_date_time_get_year (old_date),
                                       g_date_time_get_month (old_date),
                                       g_date_time_get_day_of_month (old_date),
                                       h, m,
                                       g_date_time_get_second (old_date));
-  g_date_time_unref (old_date);
 
   update_time (self);
   queue_set_datetime (self);
@@ -736,7 +708,7 @@ change_ntp (GObject         *gobject,
 static gboolean
 is_ntp_available (CcDateTimePanel *self)
 {
-  GVariant *value;
+  g_autoptr(GVariant) value = NULL;
   gboolean ntp_available = TRUE;
 
   /* We need to access this directly so that we can default to TRUE if
@@ -747,7 +719,6 @@ is_ntp_available (CcDateTimePanel *self)
     {
       if (g_variant_is_of_type (value, G_VARIANT_TYPE_BOOLEAN))
         ntp_available = g_variant_get_boolean (value);
-      g_variant_unref (value);
     }
 
   return ntp_available;
@@ -799,14 +770,13 @@ on_timedated_properties_changed (GDBusProxy       *proxy,
                                  const gchar     **invalidated_properties,
                                  CcDateTimePanel  *self)
 {
-  GError *error;
-  GVariant *variant;
-  GVariant *v;
   guint i;
 
   if (invalidated_properties != NULL)
     for (i = 0; invalidated_properties[i] != NULL; i++) {
-        error = NULL;
+        g_autoptr(GVariant) variant = NULL;
+        g_autoptr(GError) error = NULL;
+
         /* See https://bugs.freedesktop.org/show_bug.cgi?id=37632 for the reason why we're doing this */
         variant = g_dbus_proxy_call_sync (proxy,
                                           "org.freedesktop.DBus.Properties.Get",
@@ -815,13 +785,13 @@ on_timedated_properties_changed (GDBusProxy       *proxy,
                                           -1,
                                           NULL,
                                           &error);
-        if (variant == NULL) {
+        if (variant == NULL)
                 g_warning ("Failed to get property '%s': %s", invalidated_properties[i], error->message);
-                g_error_free (error);
-        } else {
+        else {
+                GVariant *v;
+
                 g_variant_get (variant, "(v)", &v);
                 g_dbus_proxy_set_cached_property (proxy, invalidated_properties[i], v);
-                g_variant_unref (variant);
         }
     }
 }
@@ -908,7 +878,8 @@ list_box_row_activated (GtkListBox      *listbox,
                         CcDateTimePanel *self)
 
 {
-  gchar *widget_name, *found;
+  g_autofree gchar *widget_name = NULL;
+  gchar *found;
 
   widget_name = g_strdup (gtk_buildable_get_name (GTK_BUILDABLE (row)));
 
@@ -932,8 +903,6 @@ list_box_row_activated (GtkListBox      *listbox,
 
       run_dialog (self, widget_name);
     }
-
-  g_free (widget_name);
 }
 
 static void
@@ -956,14 +925,13 @@ format_minutes_combobox (GtkSpinButton *spin,
                          gpointer       data)
 {
   GtkAdjustment *adjustment;
-  char *text;
+  g_autofree gchar *text = NULL;
   int value;
 
   adjustment = gtk_spin_button_get_adjustment (spin);
   value = (int)gtk_adjustment_get_value (adjustment);
   text = g_strdup_printf ("%02d", value);
   gtk_entry_set_text (GTK_ENTRY (spin), text);
-  g_free (text);
 
   return TRUE;
 }
@@ -973,7 +941,7 @@ format_hours_combobox (GtkSpinButton   *spin,
                        CcDateTimePanel *panel)
 {
   GtkAdjustment *adjustment;
-  char *text;
+  g_autofree gchar *text = NULL;
   int hour;
   gboolean use_ampm;
 
@@ -989,7 +957,6 @@ format_hours_combobox (GtkSpinButton   *spin,
   else
     text = g_strdup_printf ("%02d", hour);
   gtk_entry_set_text (GTK_ENTRY (spin), text);
-  g_free (text);
 
   return TRUE;
 }
@@ -997,7 +964,7 @@ format_hours_combobox (GtkSpinButton   *spin,
 static void
 setup_timezone_dialog (CcDateTimePanel *self)
 {
-  GtkEntryCompletion *completion;
+  g_autoptr(GtkEntryCompletion) completion = NULL;
   GtkTreeModel *completion_model;
   GtkWidget *dialog;
   GtkWidget *entry;
@@ -1017,7 +984,6 @@ setup_timezone_dialog (CcDateTimePanel *self)
   /* Create the completion object */
   completion = gtk_entry_completion_new ();
   gtk_entry_set_completion (GTK_ENTRY (entry), completion);
-  g_object_unref (completion);
 
   completion_model = GTK_TREE_MODEL (gtk_builder_get_object (self->builder,
                                                              "city-modelsort"));
@@ -1029,46 +995,37 @@ setup_timezone_dialog (CcDateTimePanel *self)
 static char *
 format_am_label ()
 {
-  GDateTime *date;
-  char *text;
+  g_autoptr(GDateTime) date = NULL;
 
   /* Construct a time at midnight, and use it to get localized AM identifier */
   date = g_date_time_new_utc (1, 1, 1, 0, 0, 0);
-  text = g_date_time_format (date, "%p");
-  g_date_time_unref (date);
-
-  return text;
+  return g_date_time_format (date, "%p");
 }
 
 static char *
 format_pm_label ()
 {
-  GDateTime *date;
-  char *text;
+  g_autoptr(GDateTime) date = NULL;
 
   /* Construct a time at noon, and use it to get localized PM identifier */
   date = g_date_time_new_utc (1, 1, 1, 12, 0, 0);
-  text = g_date_time_format (date, "%p");
-  g_date_time_unref (date);
-
-  return text;
+  return g_date_time_format (date, "%p");
 }
 
 static void
 setup_am_pm_button (CcDateTimePanel *self)
 {
-  GtkCssProvider *provider;
+  g_autoptr(GtkCssProvider) provider = NULL;
   GtkStyleContext *context;
   GtkWidget *am_pm_button;
-  char *text;
+  g_autofree gchar *am_text = NULL;
+  g_autofree gchar *pm_text = NULL;
 
-  text = format_am_label ();
-  self->am_label = gtk_label_new (text);
-  g_free (text);
+  am_text = format_am_label ();
+  self->am_label = gtk_label_new (am_text);
 
-  text = format_pm_label ();
-  self->pm_label = gtk_label_new (text);
-  g_free (text);
+  pm_text = format_pm_label ();
+  self->pm_label = gtk_label_new (pm_text);
 
   self->am_pm_stack = W ("am_pm_stack");
   gtk_container_add (GTK_CONTAINER (self->am_pm_stack), self->am_label);
@@ -1093,7 +1050,6 @@ setup_am_pm_button (CcDateTimePanel *self)
   gtk_style_context_add_provider (context,
                                   GTK_STYLE_PROVIDER (provider),
                                   GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
-  g_object_unref (provider);
 }
 
 static void
@@ -1101,7 +1057,7 @@ setup_datetime_dialog (CcDateTimePanel *self)
 {
   GtkAdjustment *adjustment;
   GdkScreen *screen;
-  GtkCssProvider *provider;
+  g_autoptr(GtkCssProvider) provider = NULL;
   GtkWidget *dialog;
   guint num_days;
 
@@ -1121,7 +1077,6 @@ setup_datetime_dialog (CcDateTimePanel *self)
   gtk_style_context_add_provider_for_screen (screen,
                                              GTK_STYLE_PROVIDER (provider),
                                              GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
-  g_object_unref (provider);
 
   dialog = W ("datetime-dialog");
   g_signal_connect (dialog, "delete-event",
@@ -1178,11 +1133,11 @@ static void
 cc_date_time_panel_init (CcDateTimePanel *self)
 {
   GtkWidget *widget;
-  GError *error;
+  g_autoptr(GError) error = NULL;
   GtkTreeModelSort *city_modelsort;
   int ret;
   const char *date_grid_name;
-  char *tmp;
+  g_autofree gchar *tmp = NULL;
 
   g_resources_register (cc_datetime_get_resource ());
 
@@ -1196,7 +1151,6 @@ cc_date_time_panel_init (CcDateTimePanel *self)
                                                 &error);
   if (self->dtm == NULL) {
         g_warning ("could not get proxy for DateTimeMechanism: %s", error->message);
-        g_clear_error (&error);
         return;
   }
 
@@ -1208,8 +1162,6 @@ cc_date_time_panel_init (CcDateTimePanel *self)
   if (ret == 0)
     {
       g_warning ("Could not load ui: %s", error ? error->message : "No reason");
-      if (error)
-        g_error_free (error);
       return;
     }
 
@@ -1232,7 +1184,6 @@ cc_date_time_panel_init (CcDateTimePanel *self)
 
   tmp = g_strdup_printf ("/org/gnome/control-center/datetime/%s.ui", date_grid_name);
   ret = gtk_builder_add_from_resource (self->builder, tmp, NULL);
-  g_free (tmp);
 
   gtk_box_pack_end (GTK_BOX (W ("time-box")), W ("date_grid"), FALSE, TRUE, 0);
 
diff --git a/panels/datetime/cc-timezone-map.c b/panels/datetime/cc-timezone-map.c
index ed416af..ad54b91 100644
--- a/panels/datetime/cc-timezone-map.c
+++ b/panels/datetime/cc-timezone-map.c
@@ -146,12 +146,7 @@ cc_timezone_map_finalize (GObject *object)
 {
   CcTimezoneMap *self = CC_TIMEZONE_MAP (object);
 
-  if (self->tzdb)
-    {
-      tz_db_free (self->tzdb);
-      self->tzdb = NULL;
-    }
-
+  g_clear_pointer (&self->tzdb, tz_db_free);
 
   G_OBJECT_CLASS (cc_timezone_map_parent_class)->finalize (object);
 }
@@ -365,10 +360,10 @@ cc_timezone_map_draw (GtkWidget *widget,
                       cairo_t   *cr)
 {
   CcTimezoneMap *map = CC_TIMEZONE_MAP (widget);
-  GdkPixbuf *hilight, *orig_hilight;
+  g_autoptr(GdkPixbuf) orig_hilight = NULL;
   GtkAllocation alloc;
-  gchar *file;
-  GError *err = NULL;
+  g_autofree gchar *file = NULL;
+  g_autoptr(GError) err = NULL;
   gdouble pointx, pointy;
   char buf[16];
 
@@ -394,26 +389,21 @@ cc_timezone_map_draw (GtkWidget *widget,
     }
 
   orig_hilight = gdk_pixbuf_new_from_resource (file, &err);
-  g_free (file);
-  file = NULL;
 
   if (!orig_hilight)
     {
       g_warning ("Could not load hilight: %s",
                  (err) ? err->message : "Unknown Error");
-      if (err)
-        g_clear_error (&err);
     }
   else
     {
+      g_autoptr(GdkPixbuf) hilight = NULL;
 
       hilight = gdk_pixbuf_scale_simple (orig_hilight, alloc.width,
                                          alloc.height, GDK_INTERP_BILINEAR);
       gdk_cairo_set_source_pixbuf (cr, hilight, 0, 0);
 
       cairo_paint (cr);
-      g_object_unref (hilight);
-      g_object_unref (orig_hilight);
     }
 
   if (map->location)
@@ -442,7 +432,7 @@ static void
 update_cursor (GtkWidget *widget)
 {
   GdkWindow *window;
-  GdkCursor *cursor = NULL;
+  g_autoptr(GdkCursor) cursor = NULL;
 
   if (!gtk_widget_get_realized (widget))
     return;
@@ -456,9 +446,6 @@ update_cursor (GtkWidget *widget)
 
   window = gtk_widget_get_window (widget);
   gdk_window_set_cursor (window, cursor);
-
-  if (cursor)
-    g_object_unref (cursor);
 }
 
 static void
@@ -517,7 +504,7 @@ static void
 set_location (CcTimezoneMap *map,
               TzLocation    *location)
 {
-  TzInfo *info;
+  g_autoptr(TzInfo) info = NULL;
 
   map->location = location;
 
@@ -527,8 +514,6 @@ set_location (CcTimezoneMap *map,
     / (60.0*60.0) + ((info->daylight) ? -1.0 : 0.0);
 
   g_signal_emit (map, signals[LOCATION_CHANGED], 0, map->location);
-
-  tz_info_free (info);
 }
 
 static gboolean
@@ -665,7 +650,7 @@ cc_timezone_map_set_timezone (CcTimezoneMap *map,
 {
   GPtrArray *locations;
   guint i;
-  char *real_tz;
+  g_autofree gchar *real_tz = NULL;
   gboolean ret;
 
   real_tz = tz_info_get_clean_name (map->tzdb, timezone);
@@ -688,8 +673,6 @@ cc_timezone_map_set_timezone (CcTimezoneMap *map,
   if (ret)
     gtk_widget_queue_draw (GTK_WIDGET (map));
 
-  g_free (real_tz);
-
   return ret;
 }
 
diff --git a/panels/datetime/test-timezone-gfx.c b/panels/datetime/test-timezone-gfx.c
index 529b7f0..2beda3f 100644
--- a/panels/datetime/test-timezone-gfx.c
+++ b/panels/datetime/test-timezone-gfx.c
@@ -7,7 +7,7 @@ static void
 test_timezone_gfx (gconstpointer data)
 {
        const char *pixmap_dir = data;
-       TzDB *db;
+       g_autoptr(TzDB) db = NULL;
        GPtrArray *locs;
        guint i;
 
@@ -16,7 +16,8 @@ test_timezone_gfx (gconstpointer data)
        for (i = 0; i < locs->len ; i++) {
                TzLocation *loc = locs->pdata[i];
                TzInfo *info;
-               char *filename, *path;
+               g_autofree gchar *filename = NULL;
+               g_autofree gchar *path = NULL;
                gdouble selected_offset;
                 char buf[16];
 
@@ -33,11 +34,7 @@ test_timezone_gfx (gconstpointer data)
                        g_message ("File '%s' missing for zone '%s'", filename, loc->zone);
                        g_test_fail ();
                }
-
-               g_free (filename);
-               g_free (path);
        }
-       tz_db_free (db);
 }
 
 int main (int argc, char **argv)
diff --git a/panels/datetime/test-timezone.c b/panels/datetime/test-timezone.c
index db5c030..ca98601 100644
--- a/panels/datetime/test-timezone.c
+++ b/panels/datetime/test-timezone.c
@@ -23,7 +23,7 @@ get_timezone_list (GList *tzs,
                return NULL;
        }
        while ((name = g_dir_read_name (dir)) != NULL) {
-               char *path;
+               g_autofree gchar *path = NULL;
 
                if (g_str_has_suffix (name, ".tab"))
                        continue;
@@ -36,10 +36,9 @@ get_timezone_list (GList *tzs,
                        if (subpath == NULL) {
                                tzs = get_timezone_list (tzs, top_path, name);
                        } else {
-                               char *new_subpath;
+                               g_autofree gchar *new_subpath = NULL;
                                new_subpath = g_strdup_printf ("%s/%s", subpath, name);
                                tzs = get_timezone_list (tzs, top_path, new_subpath);
-                               g_free (new_subpath);
                        }
                } else if (g_file_test (path, G_FILE_TEST_IS_REGULAR)) {
                        if (subpath == NULL)
@@ -50,7 +49,6 @@ get_timezone_list (GList *tzs,
                                tzs = g_list_prepend (tzs, tz);
                        }
                }
-               g_free (path);
        }
        g_dir_close (dir);
 
@@ -70,8 +68,8 @@ test_timezone (void)
        tz_db = tz_load_db ();
        tzs = get_timezone_list (NULL, TZ_DIR, NULL);
        for (l = tzs; l != NULL; l = l->next) {
-               char *timezone = l->data;
-               char *clean_tz;
+               const gchar *timezone = l->data;
+               g_autofree gchar *clean_tz = NULL;
 
                clean_tz = tz_info_get_clean_name (tz_db, timezone);
 
@@ -90,10 +88,8 @@ test_timezone (void)
                            !g_str_equal (clean_tz, "Factory"))
                                g_test_fail ();
                }
-               g_free (timezone);
-               g_free (clean_tz);
        }
-       g_list_free (tzs);
+       g_list_free_full (tzs, g_free);
        tz_db_free (tz_db);
        g_hash_table_destroy (ht);
 }
diff --git a/panels/datetime/tz.c b/panels/datetime/tz.c
index d711867..6e96dd4 100644
--- a/panels/datetime/tz.c
+++ b/panels/datetime/tz.c
@@ -48,7 +48,7 @@ static void load_backward_tz (TzDB *tz_db);
 TzDB *
 tz_load_db (void)
 {
-       gchar *tz_data_file;
+       g_autofree gchar *tz_data_file = NULL;
        TzDB *tz_db;
        FILE *tzfile;
        char buf[4096];
@@ -61,7 +61,6 @@ tz_load_db (void)
        tzfile = fopen (tz_data_file, "r");
        if (!tzfile) {
                g_warning ("Could not open *%s*\n", tz_data_file);
-               g_free (tz_data_file);
                return NULL;
        }
 
@@ -70,8 +69,10 @@ tz_load_db (void)
 
        while (fgets (buf, sizeof(buf), tzfile))
        {
-               gchar **tmpstrarr;
-               gchar *latstr, *lngstr, *p;
+               g_auto(GStrv) tmpstrarr = NULL;
+               g_autofree gchar *latstr = NULL;
+               g_autofree gchar *lngstr = NULL;
+               gchar *p;
                TzLocation *loc;
 
                if (*buf == '#') continue;
@@ -113,10 +114,6 @@ tz_load_db (void)
 #endif
 
                g_ptr_array_add (tz_db->locations, (gpointer) loc);
-
-               g_free (latstr);
-               g_free (lngstr);
-               g_strfreev (tmpstrarr);
        }
        
        fclose (tzfile);
@@ -124,8 +121,6 @@ tz_load_db (void)
        /* now sort by country */
        sort_locations_by_country (tz_db->locations);
        
-       g_free (tz_data_file);
-
        /* Load up the hashtable of backward links */
        load_backward_tz (tz_db);
 
@@ -189,12 +184,11 @@ tz_location_get_position (TzLocation *loc, double *longitude, double *latitude)
 glong
 tz_location_get_utc_offset (TzLocation *loc)
 {
-       TzInfo *tz_info;
+       g_autoptr(TzInfo) tz_info = NULL;
        glong offset;
 
        tz_info = tz_info_from_location (loc);
        offset = tz_info->utc_offset;
-       tz_info_free (tz_info);
        return offset;
 }
 
@@ -204,7 +198,7 @@ tz_info_from_location (TzLocation *loc)
        TzInfo *tzinfo;
        time_t curtime;
        struct tm *curzone;
-       gchar *tz_env_value;
+       g_autofree gchar *tz_env_value = NULL;
        
        g_return_val_if_fail (loc != NULL, NULL);
        g_return_val_if_fail (loc->zone != NULL, NULL);
@@ -244,8 +238,6 @@ tz_info_from_location (TzLocation *loc)
        else
                unsetenv ("TZ");
 
-       g_free (tz_env_value);
-       
        return tzinfo;
 }
 
@@ -295,14 +287,11 @@ compare_timezones (const char *a,
        if (g_str_equal (a, b))
                return TRUE;
        if (strchr (b, '/') == NULL) {
-               char *prefixed;
+               g_autofree gchar *prefixed = NULL;
 
                prefixed = g_strdup_printf ("/%s", b);
-               if (g_str_has_suffix (a, prefixed)) {
-                       g_free (prefixed);
+               if (g_str_has_suffix (a, prefixed))
                        return TRUE;
-               }
-               g_free (prefixed);
        }
 
        return FALSE;
@@ -428,8 +417,8 @@ sort_locations_by_country (GPtrArray *locations)
 static void
 load_backward_tz (TzDB *tz_db)
 {
-  char **lines;
-  GBytes *bytes;
+  g_auto(GStrv) lines = NULL;
+  g_autoptr(GBytes) bytes = NULL;
   const char *contents;
   guint i;
 
@@ -440,11 +429,10 @@ load_backward_tz (TzDB *tz_db)
   contents = (const char *) g_bytes_get_data (bytes, NULL);
 
   lines = g_strsplit (contents, "\n", -1);
-  g_bytes_unref (bytes);
 
   for (i = 0; lines[i] != NULL; i++)
     {
-      char **items;
+      g_auto(GStrv) items = NULL;
       guint j;
       char *real, *alias;
 
@@ -477,8 +465,6 @@ load_backward_tz (TzDB *tz_db)
         real = "Etc/GMT";
 
       g_hash_table_insert (tz_db->backward, g_strdup (alias), g_strdup (real));
-      g_strfreev (items);
     }
-  g_strfreev (lines);
 }
 
diff --git a/panels/datetime/tz.h b/panels/datetime/tz.h
index 93905b3..96b2514 100644
--- a/panels/datetime/tz.h
+++ b/panels/datetime/tz.h
@@ -85,4 +85,8 @@ gint       tz_location_set_locally    (TzLocation *loc);
 TzInfo    *tz_info_from_location      (TzLocation *loc);
 void       tz_info_free               (TzInfo *tz_info);
 
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC (TzDB, tz_db_free)
+G_DEFINE_AUTOPTR_CLEANUP_FUNC (TzInfo, tz_info_free)
+
 #endif


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