[gnome-control-center] user-accounts: Use g_autoptr/g_autofree



commit c690e921caca55dd4309d0966a91fcd36fac712e
Author: Robert Ancell <robert ancell canonical com>
Date:   Mon Nov 2 11:09:17 2020 +1300

    user-accounts: Use g_autoptr/g_autofree

 panels/user-accounts/cc-add-user-dialog.c | 174 +++++++++---------------------
 panels/user-accounts/cc-avatar-chooser.c  |  63 +++++------
 panels/user-accounts/cc-carousel.c        |   8 +-
 panels/user-accounts/cc-crop-area.c       |   5 +-
 panels/user-accounts/cc-realm-manager.c   |  98 +++++++----------
 panels/user-accounts/cc-user-image.c      |  15 +--
 panels/user-accounts/cc-user-panel.c      | 121 ++++++++-------------
 panels/user-accounts/run-passwd.c         |  45 +++-----
 panels/user-accounts/user-utils.c         |  47 +++-----
 9 files changed, 194 insertions(+), 382 deletions(-)
---
diff --git a/panels/user-accounts/cc-add-user-dialog.c b/panels/user-accounts/cc-add-user-dialog.c
index 7a99b09dd..3d1daa481 100644
--- a/panels/user-accounts/cc-add-user-dialog.c
+++ b/panels/user-accounts/cc-add-user-dialog.c
@@ -191,11 +191,10 @@ create_user_done (ActUserManager  *manager,
                   CcAddUserDialog *self)
 {
         ActUser *user;
-        GError *error;
+        g_autoptr(GError) error = NULL;
 
         /* Note that user is returned without an extra reference */
 
-        error = NULL;
         user = act_user_manager_create_user_finish (manager, res, &error);
 
         if (user == NULL) {
@@ -203,7 +202,6 @@ create_user_done (ActUserManager  *manager,
                 g_debug ("Failed to create user: %s", error->message);
                 if (!g_error_matches (error, ACT_USER_MANAGER_ERROR, 
ACT_USER_MANAGER_ERROR_PERMISSION_DENIED))
                        show_error_dialog (self, _("Failed to add account"), error);
-                g_error_free (error);
                 gtk_widget_grab_focus (GTK_WIDGET (self->local_name_entry));
         } else {
                 g_debug ("Created user: %s", act_user_get_user_name (user));
@@ -311,7 +309,7 @@ static void local_username_is_valid_cb (GObject *source_object,
                                         GAsyncResult *result,
                                         gpointer user_data)
 {
-        CcAddUserDialog *self = CC_ADD_USER_DIALOG (user_data);
+        g_autoptr(CcAddUserDialog) self = CC_ADD_USER_DIALOG (user_data);
         g_autoptr(GError) error = NULL;
         g_autofree gchar *tip = NULL;
         g_autofree gchar *name = NULL;
@@ -330,8 +328,6 @@ static void local_username_is_valid_cb (GObject *source_object,
                 gtk_label_set_label (self->local_username_hint_label, tip);
                 dialog_validate (self);
         }
-
-        g_object_unref (self);
 }
 
 static gboolean
@@ -412,17 +408,23 @@ generate_username_choices (const gchar  *name,
                            GtkListStore *store)
 {
         gboolean in_use, same_as_initial;
-        char *lc_name, *ascii_name, *stripped_name;
-        char **words1;
-        char **words2 = NULL;
+        g_autofree gchar *lc_name = NULL;
+        g_autofree gchar *ascii_name = NULL;
+        g_autofree gchar *stripped_name = NULL;
+        g_auto(GStrv) words1 = NULL;
         char **w1, **w2;
         char *c;
         char *unicode_fallback = "?";
-        GString *first_word, *last_word;
-        GString *item0, *item1, *item2, *item3, *item4;
+        g_autoptr(GString) first_word = NULL;
+        g_autoptr(GString) last_word = NULL;
+        g_autoptr(GString) item0 = NULL;
+        g_autoptr(GString) item1 = NULL;
+        g_autoptr(GString) item2 = NULL;
+        g_autoptr(GString) item3 = NULL;
+        g_autoptr(GString) item4 = NULL;
         int len;
         int nwords1, nwords2, i;
-        GHashTable *items;
+        g_autoptr(GHashTable) items = NULL;
         GtkTreeIter iter;
         gsize max_name_length;
 
@@ -454,9 +456,6 @@ generate_username_choices (const gchar  *name,
         }
 
         if (strlen (stripped_name) == 0) {
-                g_free (ascii_name);
-                g_free (lc_name);
-                g_free (stripped_name);
                 return;
         }
 
@@ -470,10 +469,6 @@ generate_username_choices (const gchar  *name,
         /* The default item is a concatenation of all words without ? */
         item0 = g_string_sized_new (strlen (stripped_name));
 
-        g_free (ascii_name);
-        g_free (lc_name);
-        g_free (stripped_name);
-
         /* Concatenate the whole first word with the first letter of each
          * word (item1), and the last word with the first letter of each
          * word (item2). item3 and item4 are symmetrical respectively to
@@ -496,6 +491,8 @@ generate_username_choices (const gchar  *name,
         nwords1 = 0;
         nwords2 = 0;
         for (w1 = words1; *w1; w1++) {
+                g_auto(GStrv) words2 = NULL;
+
                 if (strlen (*w1) == 0)
                         continue;
 
@@ -543,8 +540,6 @@ generate_username_choices (const gchar  *name,
                         /* always save current word so that we have it if last one reveals empty */
                         last_word = g_string_append (last_word, *w2);
                 }
-
-                g_strfreev (words2);
         }
         item2 = g_string_append (item2, last_word->str);
         item3 = g_string_append (item3, first_word->str);
@@ -623,16 +618,6 @@ generate_username_choices (const gchar  *name,
                         g_hash_table_insert (items, first_word->str, first_word->str);
                 }
         }
-
-        g_hash_table_destroy (items);
-        g_strfreev (words1);
-        g_string_free (first_word, TRUE);
-        g_string_free (last_word, TRUE);
-        g_string_free (item0, TRUE);
-        g_string_free (item1, TRUE);
-        g_string_free (item2, TRUE);
-        g_string_free (item3, TRUE);
-        g_string_free (item4, TRUE);
 }
 
 static void
@@ -684,7 +669,7 @@ update_password_match (CcAddUserDialog *self)
 static void
 local_password_entry_icon_press_cb (CcAddUserDialog *self)
 {
-        gchar *pwd;
+        g_autofree gchar *pwd = NULL;
 
         pwd = pw_generate ();
         if (pwd == NULL)
@@ -694,8 +679,6 @@ local_password_entry_icon_press_cb (CcAddUserDialog *self)
         gtk_entry_set_text (self->local_verify_entry, pwd);
         gtk_entry_set_visibility (self->local_password_entry, TRUE);
         gtk_widget_set_sensitive (GTK_WIDGET (self->local_verify_entry), TRUE);
-
-        g_free (pwd);
 }
 
 static gboolean
@@ -813,11 +796,10 @@ enterprise_add_realm (CcAddUserDialog *self,
 {
         GtkTreeModel *model;
         GtkTreeIter iter;
-        CcRealmCommon *common;
+        g_autoptr(CcRealmCommon) common = NULL;
         const gchar *realm_name;
         gboolean match;
         gboolean ret;
-        gchar *name;
 
         common = cc_realm_object_get_common (realm);
         g_return_if_fail (common != NULL);
@@ -834,12 +816,12 @@ enterprise_add_realm (CcAddUserDialog *self,
         model = GTK_TREE_MODEL (self->enterprise_realm_model);
         ret = gtk_tree_model_get_iter_first (model, &iter);
         while (ret) {
+                g_autofree gchar *name = NULL;
+
                 gtk_tree_model_get (model, &iter, 0, &name, -1);
                 match = (g_strcmp0 (name, realm_name) == 0);
-                g_free (name);
                 if (match) {
                         g_debug ("ignoring duplicate realm: %s", realm_name);
-                        g_object_unref (common);
                         return;
                 }
                 ret = gtk_tree_model_iter_next (model, &iter);
@@ -858,8 +840,6 @@ enterprise_add_realm (CcAddUserDialog *self,
 
         g_debug ("added realm to drop down: %s %s", realm_name,
                  g_dbus_object_get_object_path (G_DBUS_OBJECT (realm)));
-
-        g_object_unref (common);
 }
 
 static void
@@ -875,12 +855,11 @@ on_register_user (GObject *source,
                   GAsyncResult *result,
                   gpointer user_data)
 {
-        CcAddUserDialog *self = CC_ADD_USER_DIALOG (user_data);
-        GError *error = NULL;
+        g_autoptr(CcAddUserDialog) self = CC_ADD_USER_DIALOG (user_data);
+        g_autoptr(GError) error = NULL;
         ActUser *user;
 
         if (g_cancellable_is_cancelled (self->cancellable)) {
-                g_object_unref (self);
                 return;
         }
 
@@ -896,10 +875,7 @@ on_register_user (GObject *source,
                 show_error_dialog (self, _("Failed to register account"), error);
                 g_message ("Couldn't cache user account: %s", error->message);
                 finish_action (self);
-                g_error_free (error);
         }
-
-        g_object_unref (self);
 }
 
 static void
@@ -907,20 +883,18 @@ on_permit_user_login (GObject *source,
                       GAsyncResult *result,
                       gpointer user_data)
 {
-        CcAddUserDialog *self = CC_ADD_USER_DIALOG (user_data);
+        g_autoptr(CcAddUserDialog) self = CC_ADD_USER_DIALOG (user_data);
         CcRealmCommon *common;
         ActUserManager *manager;
-        GError *error = NULL;
-        gchar *login;
+        g_autoptr(GError) error = NULL;
 
         if (g_cancellable_is_cancelled (self->cancellable)) {
-                g_object_unref (self);
                 return;
         }
 
         common = CC_REALM_COMMON (source);
-        cc_realm_common_call_change_login_policy_finish (common, result, &error);
-        if (error == NULL) {
+        if (cc_realm_common_call_change_login_policy_finish (common, result, &error)) {
+                g_autofree gchar *login = NULL;
 
                 /*
                  * Now tell the account service about this user. The account service
@@ -936,23 +910,18 @@ on_permit_user_login (GObject *source,
                 act_user_manager_cache_user_async (manager, login, self->cancellable,
                                                    on_register_user, g_object_ref (self));
 
-                g_free (login);
-
         } else {
                 show_error_dialog (self, _("Failed to register account"), error);
                 g_message ("Couldn't permit logins on account: %s", error->message);
                 finish_action (self);
-                g_error_free (error);
         }
-
-        g_object_unref (self);
 }
 
 static void
 enterprise_permit_user_login (CcAddUserDialog *self)
 {
-        CcRealmCommon *common;
-        gchar *login;
+        g_autoptr(CcRealmCommon) common = NULL;
+        g_autofree gchar *login = NULL;
         const gchar *add[2];
         const gchar *remove[1];
         GVariant *options;
@@ -980,9 +949,6 @@ enterprise_permit_user_login (CcAddUserDialog *self)
                                                   self->cancellable,
                                                   on_permit_user_login,
                                                   g_object_ref (self));
-
-        g_object_unref (common);
-        g_free (login);
 }
 
 static void
@@ -1011,8 +977,8 @@ static void
 join_show_prompt (CcAddUserDialog *self,
                   GError          *error)
 {
-        CcRealmKerberosMembership *membership;
-        CcRealmKerberos *kerberos;
+        g_autoptr(CcRealmKerberosMembership) membership = NULL;
+        g_autoptr(CcRealmKerberos) kerberos = NULL;
         const gchar *name;
 
         gtk_entry_set_text (self->join_password, "");
@@ -1052,8 +1018,6 @@ join_show_prompt (CcAddUserDialog *self,
         gtk_window_present (GTK_WINDOW (self->join_dialog));
 
         self->join_prompted = TRUE;
-        g_object_unref (kerberos);
-        g_object_unref (membership);
 
         /* And now we wait for on_join_response() */
 }
@@ -1063,19 +1027,18 @@ on_join_login (GObject *source,
                GAsyncResult *result,
                gpointer user_data)
 {
-        CcAddUserDialog *self = CC_ADD_USER_DIALOG (user_data);
-        GError *error = NULL;
-        GBytes *creds;
+        g_autoptr(CcAddUserDialog) self = CC_ADD_USER_DIALOG (user_data);
+        g_autoptr(GError) error = NULL;
+        g_autoptr(GBytes) creds = NULL;
 
         if (g_cancellable_is_cancelled (self->cancellable)) {
-                g_object_unref (self);
                 return;
         }
 
         creds = cc_realm_login_finish (result, &error);
 
         /* Logged in as admin successfully, use creds to join domain */
-        if (error == NULL) {
+        if (creds != NULL) {
                 if (!cc_realm_join_as_admin (self->selected_realm,
                                              gtk_entry_get_text (self->join_name),
                                              gtk_entry_get_text (self->join_password),
@@ -1086,23 +1049,18 @@ on_join_login (GObject *source,
                         finish_action (self);
                 }
 
-                g_bytes_unref (creds);
-
         /* Couldn't login as admin, show prompt again */
         } else {
                 join_show_prompt (self, error);
                 g_message ("Couldn't log in as admin to join domain: %s", error->message);
-                g_error_free (error);
         }
-
-        g_object_unref (self);
 }
 
 static void
 join_init (CcAddUserDialog *self)
 {
-        GtkBuilder *builder;
-        GError *error = NULL;
+        g_autoptr(GtkBuilder) builder = NULL;
+        g_autoptr(GError) error = NULL;
 
         builder = gtk_builder_new ();
 
@@ -1110,7 +1068,6 @@ join_init (CcAddUserDialog *self)
                                             "/org/gnome/control-center/user-accounts/join-dialog.ui",
                                             &error)) {
                 g_error ("%s", error->message);
-                g_error_free (error);
                 return;
         }
 
@@ -1121,8 +1078,6 @@ join_init (CcAddUserDialog *self)
 
         g_signal_connect_object (self->join_dialog, "response",
                                  G_CALLBACK (on_join_response), self, G_CONNECT_SWAPPED);
-
-        g_object_unref (builder);
 }
 
 static void
@@ -1130,11 +1085,10 @@ on_realm_joined (GObject *source,
                  GAsyncResult *result,
                  gpointer user_data)
 {
-        CcAddUserDialog *self = CC_ADD_USER_DIALOG (user_data);
-        GError *error = NULL;
+        g_autoptr(CcAddUserDialog) self = CC_ADD_USER_DIALOG (user_data);
+        g_autoptr(GError) error = NULL;
 
         if (g_cancellable_is_cancelled (self->cancellable)) {
-                g_object_unref (self);
                 return;
         }
 
@@ -1158,9 +1112,6 @@ on_realm_joined (GObject *source,
                 g_message ("Failed to join the domain: %s", error->message);
                 finish_action (self);
         }
-
-        g_clear_error (&error);
-        g_object_unref (self);
 }
 
 static void
@@ -1168,13 +1119,12 @@ on_realm_login (GObject *source,
                 GAsyncResult *result,
                 gpointer user_data)
 {
-        CcAddUserDialog *self = CC_ADD_USER_DIALOG (user_data);
-        GError *error = NULL;
-        GBytes *creds = NULL;
+        g_autoptr(CcAddUserDialog) self = CC_ADD_USER_DIALOG (user_data);
+        g_autoptr(GError) error = NULL;
+        g_autoptr(GBytes) creds = NULL;
         const gchar *message;
 
         if (g_cancellable_is_cancelled (self->cancellable)) {
-                g_object_unref (self);
                 return;
         }
 
@@ -1210,8 +1160,6 @@ on_realm_login (GObject *source,
                         join_show_prompt (self, NULL);
                 }
 
-                g_bytes_unref (creds);
-
         /* A problem with the user's login name or password */
         } else if (g_error_matches (error, CC_REALM_ERROR, CC_REALM_ERROR_BAD_LOGIN)) {
                 g_debug ("Problem with the user's login: %s", error->message);
@@ -1234,9 +1182,6 @@ on_realm_login (GObject *source,
                 g_message ("Couldn't log in as user: %s", error->message);
                 finish_action (self);
         }
-
-        g_clear_error (&error);
-        g_object_unref (self);
 }
 
 static void
@@ -1257,13 +1202,11 @@ on_realm_discover_input (GObject *source,
                          GAsyncResult *result,
                          gpointer user_data)
 {
-        CcAddUserDialog *self = CC_ADD_USER_DIALOG (user_data);
-        GError *error = NULL;
+        g_autoptr(CcAddUserDialog) self = CC_ADD_USER_DIALOG (user_data);
+        g_autoptr(GError) error = NULL;
         GList *realms;
-        gchar *message;
 
         if (g_cancellable_is_cancelled (self->cancellable)) {
-                g_object_unref (self);
                 return;
         }
 
@@ -1284,6 +1227,8 @@ on_realm_discover_input (GObject *source,
 
         /* The domain is likely invalid*/
         } else {
+                g_autofree gchar *message = NULL;
+
                 g_message ("Couldn't discover domain: %s", error->message);
                 g_dbus_error_strip_remote_error (error);
 
@@ -1294,9 +1239,6 @@ on_realm_discover_input (GObject *source,
                 }
                 gtk_label_set_text (self->enterprise_domain_hint_label, message);
 
-                g_free (message);
-                g_error_free (error);
-
                 if (self->enterprise_check_credentials) {
                         finish_action (self);
                         self->enterprise_check_credentials = FALSE;
@@ -1307,8 +1249,6 @@ on_realm_discover_input (GObject *source,
                 finish_action (self);
                 dialog_validate (self);
         }
-
-        g_object_unref (self);
 }
 
 static void
@@ -1349,8 +1289,7 @@ clear_realm_manager (CcAddUserDialog *self)
                 g_signal_handlers_disconnect_by_func (self->realm_manager,
                                                       on_manager_realm_added,
                                                       self);
-                g_object_unref (self->realm_manager);
-                self->realm_manager = NULL;
+                g_clear_object (&self->realm_manager);
         }
 }
 
@@ -1359,8 +1298,8 @@ on_realm_manager_created (GObject *source,
                           GAsyncResult *result,
                           gpointer user_data)
 {
-        CcAddUserDialog *self = CC_ADD_USER_DIALOG (user_data);
-        GError *error = NULL;
+        g_autoptr(CcAddUserDialog) self = CC_ADD_USER_DIALOG (user_data);
+        g_autoptr(GError) error = NULL;
         GList *realms, *l;
 
         clear_realm_manager (self);
@@ -1368,13 +1307,10 @@ on_realm_manager_created (GObject *source,
         self->realm_manager = cc_realm_manager_new_finish (result, &error);
         if (error != NULL) {
                 g_warning ("Couldn't contact realmd service: %s", error->message);
-                g_object_unref (self);
-                g_error_free (error);
                 return;
         }
 
         if (g_cancellable_is_cancelled (self->cancellable)) {
-                g_object_unref (self);
                 return;
         }
 
@@ -1393,7 +1329,6 @@ on_realm_manager_created (GObject *source,
         /* Show the 'Enterprise Login' stuff, and update mode */
         gtk_widget_show (GTK_WIDGET (self->enterprise_button));
         mode_change (self, self->mode);
-        g_object_unref (self);
 }
 
 static void
@@ -1595,8 +1530,8 @@ on_permission_acquired (GObject *source_object,
                         GAsyncResult *res,
                         gpointer user_data)
 {
-        CcAddUserDialog *self = CC_ADD_USER_DIALOG (user_data);
-        GError *error = NULL;
+        g_autoptr(CcAddUserDialog) self = CC_ADD_USER_DIALOG (user_data);
+        g_autoptr(GError) error = NULL;
 
         /* Paired with begin_action in cc_add_user_dialog_response () */
         finish_action (self);
@@ -1607,9 +1542,6 @@ on_permission_acquired (GObject *source_object,
         } else if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
                 g_warning ("Failed to acquire permission: %s", error->message);
         }
-
-        g_clear_error (&error);
-        g_object_unref (self);
 }
 
 static void
@@ -1653,8 +1585,7 @@ cc_add_user_dialog_dispose (GObject *obj)
                 g_signal_handlers_disconnect_by_func (self->realm_manager,
                                                       on_manager_realm_added,
                                                       self);
-                g_object_unref (self->realm_manager);
-                self->realm_manager = NULL;
+                g_clear_object (&self->realm_manager);
         }
 
         if (self->local_password_timeout_id != 0) {
@@ -1687,8 +1618,7 @@ cc_add_user_dialog_finalize (GObject *obj)
 {
         CcAddUserDialog *self = CC_ADD_USER_DIALOG (obj);
 
-        if (self->cancellable)
-                g_object_unref (self->cancellable);
+        g_clear_object (&self->cancellable);
         g_clear_object (&self->permission);
 
         G_OBJECT_CLASS (cc_add_user_dialog_parent_class)->finalize (obj);
diff --git a/panels/user-accounts/cc-avatar-chooser.c b/panels/user-accounts/cc-avatar-chooser.c
index d0d4e1bbe..1c96854b3 100644
--- a/panels/user-accounts/cc-avatar-chooser.c
+++ b/panels/user-accounts/cc-avatar-chooser.c
@@ -73,7 +73,8 @@ crop_dialog_response (CcAvatarChooser *self,
                       gint             response_id,
                       GtkWidget       *dialog)
 {
-        GdkPixbuf *pb, *pb2;
+        g_autoptr(GdkPixbuf) pb = NULL;
+        g_autoptr(GdkPixbuf) pb2 = NULL;
 
         if (response_id != GTK_RESPONSE_ACCEPT) {
                 self->crop_area = NULL;
@@ -86,9 +87,6 @@ crop_dialog_response (CcAvatarChooser *self,
 
         set_user_icon_data (self->user, pb2);
 
-        g_object_unref (pb2);
-        g_object_unref (pb);
-
         self->crop_area = NULL;
         gtk_widget_destroy (dialog);
 
@@ -136,9 +134,10 @@ file_chooser_response (CcAvatarChooser *self,
                        gint             response,
                        GtkDialog       *chooser)
 {
-        gchar *filename;
-        GError *error;
-        GdkPixbuf *pixbuf, *pixbuf2;
+        g_autofree gchar *filename = NULL;
+        g_autoptr(GError) error = NULL;
+        g_autoptr(GdkPixbuf) pixbuf = NULL;
+        g_autoptr(GdkPixbuf) pixbuf2 = NULL;
 
         if (response != GTK_RESPONSE_ACCEPT) {
                 gtk_widget_destroy (GTK_WIDGET (chooser));
@@ -147,36 +146,31 @@ file_chooser_response (CcAvatarChooser *self,
 
         filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (chooser));
 
-        error = NULL;
         pixbuf = gdk_pixbuf_new_from_file (filename, &error);
         if (pixbuf == NULL) {
                 g_warning ("Failed to load %s: %s", filename, error->message);
-                g_error_free (error);
         }
-        g_free (filename);
 
         pixbuf2 = gdk_pixbuf_apply_embedded_orientation (pixbuf);
-        g_object_unref (pixbuf);
 
         gtk_widget_destroy (GTK_WIDGET (chooser));
 
         cc_avatar_chooser_crop (self, pixbuf2);
-        g_object_unref (pixbuf2);
 }
 
 static void
 update_preview (GtkFileChooser               *chooser,
                 GnomeDesktopThumbnailFactory *thumb_factory)
 {
-        gchar *uri;
+        g_autofree gchar *uri = NULL;
 
         uri = gtk_file_chooser_get_uri (chooser);
 
         if (uri) {
-                GdkPixbuf *pixbuf = NULL;
-                char *mime_type = NULL;
-                GFile *file;
-                GFileInfo *file_info;
+                g_autoptr(GdkPixbuf) pixbuf = NULL;
+                g_autofree char *mime_type = NULL;
+                g_autoptr(GFile) file = NULL;
+                g_autoptr(GFileInfo) file_info = NULL;
                 GtkWidget *preview;
 
                 preview = gtk_file_chooser_get_preview_widget (chooser);
@@ -186,19 +180,16 @@ update_preview (GtkFileChooser               *chooser,
                                                "standard::*",
                                                G_FILE_QUERY_INFO_NONE,
                                                NULL, NULL);
-                g_object_unref (file);
 
                 if (file_info != NULL &&
                     g_file_info_get_file_type (file_info) != G_FILE_TYPE_DIRECTORY) {
                         mime_type = g_strdup (g_file_info_get_content_type (file_info));
-                        g_object_unref (file_info);
                 }
 
                 if (mime_type) {
                         pixbuf = gnome_desktop_thumbnail_factory_generate_thumbnail (thumb_factory,
                                                                                      uri,
                                                                                      mime_type);
-                        g_free (mime_type);
                 }
 
                 gtk_dialog_set_response_sensitive (GTK_DIALOG (chooser),
@@ -207,15 +198,12 @@ update_preview (GtkFileChooser               *chooser,
 
                 if (pixbuf != NULL) {
                         gtk_image_set_from_pixbuf (GTK_IMAGE (preview), pixbuf);
-                        g_object_unref (pixbuf);
                 }
                 else {
                         gtk_image_set_from_icon_name (GTK_IMAGE (preview),
                                                       "dialog-question",
                                                       GTK_ICON_SIZE_DIALOG);
                 }
-
-                g_free (uri);
         }
 
         gtk_file_chooser_set_preview_widget_active (chooser, TRUE);
@@ -280,15 +268,13 @@ webcam_response_cb (CcAvatarChooser  *self,
                     GtkDialog        *dialog)
 {
         if (response == GTK_RESPONSE_ACCEPT) {
-                GdkPixbuf *pb, *pb2;
+                g_autoptr(GdkPixbuf) pb = NULL;
+                g_autoptr(GdkPixbuf) pb2 = NULL;
 
                 g_object_get (G_OBJECT (dialog), "pixbuf", &pb, NULL);
                 pb2 = gdk_pixbuf_scale_simple (pb, PIXEL_SIZE, PIXEL_SIZE, GDK_INTERP_BILINEAR);
 
                 set_user_icon_data (self->user, pb2);
-
-                g_object_unref (pb2);
-                g_object_unref (pb);
         }
         if (response != GTK_RESPONSE_DELETE_EVENT &&
             response != GTK_RESPONSE_NONE)
@@ -448,15 +434,16 @@ get_system_facesdirs (void)
 static gboolean
 add_faces_from_dirs (GListStore *faces, GStrv facesdirs, gboolean add_all)
 {
-        GFile *file, *dir;
-        GFileInfo *info;
-        GFileEnumerator *enumerator;
+        GFile *file;
         GFileType type;
         const gchar *target;
         guint i;
         gboolean added_faces = FALSE;
 
         for (i = 0; facesdirs[i] != NULL; i++) {
+                g_autoptr(GFile) dir = NULL;
+                g_autoptr(GFileEnumerator) enumerator = NULL;
+
                 dir = g_file_new_for_path (facesdirs[i]);
 
                 enumerator = g_file_enumerate_children (dir,
@@ -467,34 +454,33 @@ add_faces_from_dirs (GListStore *faces, GStrv facesdirs, gboolean add_all)
                                                         G_FILE_QUERY_INFO_NONE,
                                                         NULL, NULL);
                 if (enumerator == NULL) {
-                        g_object_unref (dir);
                         continue;
                 }
 
-                while ((info = g_file_enumerator_next_file (enumerator, NULL, NULL)) != NULL) {
+                while (TRUE) {
+                        g_autoptr(GFileInfo) info = g_file_enumerator_next_file (enumerator, NULL, NULL);
+                        if (info == NULL) {
+                                break;
+                        }
+
                         type = g_file_info_get_file_type (info);
                         if (type != G_FILE_TYPE_REGULAR &&
                             type != G_FILE_TYPE_SYMBOLIC_LINK) {
-                                g_object_unref (info);
                                 continue;
                         }
 
                         target = g_file_info_get_symlink_target (info);
                         if (target != NULL && g_str_has_prefix (target , "legacy/")) {
-                                g_object_unref (info);
                                 continue;
                         }
 
                         file = g_file_get_child (dir, g_file_info_get_name (info));
                         g_list_store_append (faces, file);
 
-                        g_object_unref (info);
                         added_faces = TRUE;
                 }
 
                 g_file_enumerator_close (enumerator, NULL, NULL);
-                g_object_unref (enumerator);
-                g_object_unref (dir);
 
                 if (added_faces && !add_all)
                         break;
@@ -647,8 +633,7 @@ cc_avatar_chooser_set_user (CcAvatarChooser *self,
 
         if (self->user) {
                 gtk_container_foreach (GTK_CONTAINER (self->user_flowbox), (GtkCallback) gtk_widget_destroy, 
NULL);
-                g_object_unref (self->user);
-                self->user = NULL;
+                g_clear_object (&self->user);
         }
         self->user = g_object_ref (user);
 
diff --git a/panels/user-accounts/cc-carousel.c b/panels/user-accounts/cc-carousel.c
index 2c3cd99b7..3aa2a9b1a 100644
--- a/panels/user-accounts/cc-carousel.c
+++ b/panels/user-accounts/cc-carousel.c
@@ -110,7 +110,7 @@ static void
 cc_carousel_move_arrow (CcCarousel *self)
 {
         GtkStyleContext *context;
-        gchar *css;
+        g_autofree gchar *css = NULL;
         gint end_x;
         GtkSettings *settings;
         gboolean animations;
@@ -149,8 +149,6 @@ cc_carousel_move_arrow (CcCarousel *self)
         self->provider = GTK_STYLE_PROVIDER (gtk_css_provider_new ());
         gtk_css_provider_load_from_data (GTK_CSS_PROVIDER (self->provider), css, -1, NULL);
         gtk_style_context_add_provider (context, self->provider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
-
-        g_free (css);
 }
 
 static gint
@@ -413,7 +411,7 @@ on_transition_running (CcCarousel *self)
 static void
 cc_carousel_init (CcCarousel *self)
 {
-        GtkStyleProvider *provider;
+        g_autoptr(GtkStyleProvider) provider = NULL;
 
         gtk_widget_init_template (GTK_WIDGET (self));
 
@@ -425,8 +423,6 @@ cc_carousel_init (CcCarousel *self)
                                                    provider,
                                                    GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
 
-        g_object_unref (provider);
-
         g_signal_connect_object (self->stack, "size-allocate", G_CALLBACK (on_size_allocate), self, 
G_CONNECT_SWAPPED);
         g_signal_connect_object (self->stack, "notify::transition-running", G_CALLBACK 
(on_transition_running), self, G_CONNECT_SWAPPED);
 }
diff --git a/panels/user-accounts/cc-crop-area.c b/panels/user-accounts/cc-crop-area.c
index fc7cb621d..d80f90a80 100644
--- a/panels/user-accounts/cc-crop-area.c
+++ b/panels/user-accounts/cc-crop-area.c
@@ -763,10 +763,7 @@ cc_crop_area_set_picture (CcCropArea *area,
         int width;
         int height;
 
-        if (area->browse_pixbuf) {
-                g_object_unref (area->browse_pixbuf);
-                area->browse_pixbuf = NULL;
-        }
+        g_clear_object (&area->browse_pixbuf);
         if (pixbuf) {
                 area->browse_pixbuf = g_object_ref (pixbuf);
                 width = gdk_pixbuf_get_width (pixbuf);
diff --git a/panels/user-accounts/cc-realm-manager.c b/panels/user-accounts/cc-realm-manager.c
index 9b8077e49..bc43e6d0f 100644
--- a/panels/user-accounts/cc-realm-manager.c
+++ b/panels/user-accounts/cc-realm-manager.c
@@ -62,20 +62,19 @@ cc_realm_error_get_quark (void)
 static gboolean
 is_realm_with_kerberos_and_membership (gpointer object)
 {
-        GDBusInterface *interface;
+        g_autoptr(GDBusInterface) kerberos_interface = NULL;
+        g_autoptr(GDBusInterface) kerberos_membership_interface = NULL;
 
         if (!G_IS_DBUS_OBJECT (object))
                 return FALSE;
 
-        interface = g_dbus_object_get_interface (object, "org.freedesktop.realmd.Kerberos");
-        if (interface == NULL)
+        kerberos_interface = g_dbus_object_get_interface (object, "org.freedesktop.realmd.Kerberos");
+        if (kerberos_interface == NULL)
                 return FALSE;
-        g_object_unref (interface);
 
-        interface = g_dbus_object_get_interface (object, "org.freedesktop.realmd.KerberosMembership");
-        if (interface == NULL)
+        kerberos_membership_interface = g_dbus_object_get_interface (object, 
"org.freedesktop.realmd.KerberosMembership");
+        if (kerberos_membership_interface == NULL)
                 return FALSE;
-        g_object_unref (interface);
 
         return TRUE;
 }
@@ -167,7 +166,7 @@ on_provider_new (GObject *source,
                  GAsyncResult *result,
                  gpointer user_data)
 {
-        GTask *task = G_TASK (user_data);
+        g_autoptr(GTask) task = G_TASK (user_data);
         CcRealmManager *manager = g_task_get_task_data (task);
         GError *error = NULL;
 
@@ -179,8 +178,6 @@ on_provider_new (GObject *source,
         } else {
                 g_task_return_error (task, error);
         }
-
-        g_object_unref (task);
 }
 
 static void
@@ -188,7 +185,7 @@ on_manager_new (GObject *source,
                 GAsyncResult *result,
                 gpointer user_data)
 {
-        GTask *task = G_TASK (user_data);
+        g_autoptr(GTask) task = G_TASK (user_data);
         CcRealmManager *manager;
         GDBusConnection *connection;
         GError *error = NULL;
@@ -222,9 +219,9 @@ on_manager_new (GObject *source,
                                              "/org/freedesktop/realmd",
                                              g_task_get_cancellable (task),
                                              on_provider_new, task);
+                g_steal_pointer (&task);
         } else {
                 g_task_return_error (task, error);
-                g_object_unref (task);
         }
 }
 
@@ -271,9 +268,8 @@ on_provider_discover (GObject *source,
                       GAsyncResult *result,
                       gpointer user_data)
 {
-        GTask *task = G_TASK (user_data);
+        g_autoptr(GTask) task = G_TASK (user_data);
         CcRealmManager *manager = g_task_get_source_object (task);
-        GDBusObject *object;
         GError *error = NULL;
         gboolean no_membership = FALSE;
         gchar **realms;
@@ -285,17 +281,18 @@ on_provider_discover (GObject *source,
                                                 &realms, result, &error);
         if (error == NULL) {
                 for (i = 0; realms[i]; i++) {
+                        g_autoptr(GDBusObject) object = NULL;
+
                         object = g_dbus_object_manager_get_object (G_DBUS_OBJECT_MANAGER (manager), 
realms[i]);
                         if (object == NULL) {
                                 g_warning ("Realm is not in object manager: %s", realms[i]);
                         } else {
                                 if (is_realm_with_kerberos_and_membership (object)) {
                                         g_debug ("Discovered realm: %s", realms[i]);
-                                        kerberos_realms = g_list_prepend (kerberos_realms, object);
+                                        kerberos_realms = g_list_prepend (kerberos_realms, g_steal_pointer 
(&object));
                                 } else {
                                         g_debug ("Realm does not support kerberos membership: %s", 
realms[i]);
                                         no_membership = TRUE;
-                                        g_object_unref (object);
                                 }
                         }
                 }
@@ -314,8 +311,6 @@ on_provider_discover (GObject *source,
         } else {
                 g_task_return_error (task, error);
         }
-
-        g_object_unref (task);
 }
 
 void
@@ -395,26 +390,23 @@ gchar *
 cc_realm_calculate_login (CcRealmCommon *realm,
                           const gchar *username)
 {
-        GString *string;
         const gchar *const *formats;
-        gchar *login = NULL;
 
         formats = cc_realm_common_get_login_formats (realm);
         if (formats[0] != NULL) {
-                string = g_string_new (formats[0]);
+                GString *string = g_string_new (formats[0]);
                 string_replace (string, "%U", username);
                 string_replace (string, "%D", cc_realm_common_get_name (realm));
-                login = g_string_free (string, FALSE);
+                return g_string_free (string, FALSE);
         }
 
-        return login;
-
+        return NULL;
 }
 
 gboolean
 cc_realm_is_configured (CcRealmObject *realm)
 {
-        CcRealmCommon *common;
+        g_autoptr(CcRealmCommon) common = NULL;
         const gchar *configured;
         gboolean is = FALSE;
 
@@ -422,7 +414,6 @@ cc_realm_is_configured (CcRealmObject *realm)
         if (common != NULL) {
                 configured = cc_realm_common_get_configured (common);
                 is = configured != NULL && !g_str_equal (configured, "");
-                g_object_unref (common);
         }
 
         return is;
@@ -463,7 +454,7 @@ realm_join_as_owner (CcRealmObject *realm,
                      GAsyncReadyCallback callback,
                      gpointer user_data)
 {
-        CcRealmKerberosMembership *membership;
+        g_autoptr(CcRealmKerberosMembership) membership = NULL;
         GVariant *contents;
         GVariant *options;
         GVariant *option;
@@ -476,7 +467,6 @@ realm_join_as_owner (CcRealmObject *realm,
         type = find_supported_credentials (membership, owner);
         if (type == NULL) {
                 g_debug ("Couldn't find supported credential type for owner: %s", owner);
-                g_object_unref (membership);
                 return FALSE;
         }
 
@@ -503,7 +493,6 @@ realm_join_as_owner (CcRealmObject *realm,
 
         cc_realm_kerberos_membership_call_join (membership, creds, options,
                                                 cancellable, callback, user_data);
-        g_object_unref (membership);
 
         return TRUE;
 }
@@ -553,9 +542,9 @@ cc_realm_join_finish (CcRealmObject *realm,
                       GAsyncResult *result,
                       GError **error)
 {
-        CcRealmKerberosMembership *membership;
-        GError *call_error = NULL;
-        gchar *dbus_error;
+        g_autoptr(CcRealmKerberosMembership) membership = NULL;
+        g_autoptr(GError) call_error = NULL;
+        g_autofree gchar *dbus_error = NULL;
 
         g_return_val_if_fail (CC_REALM_IS_OBJECT (realm), FALSE);
         g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
@@ -563,10 +552,7 @@ cc_realm_join_finish (CcRealmObject *realm,
         membership = cc_realm_object_get_kerberos_membership (realm);
         g_return_val_if_fail (membership != NULL, FALSE);
 
-        cc_realm_kerberos_membership_call_join_finish (membership, result, &call_error);
-        g_object_unref (membership);
-
-        if (call_error == NULL) {
+        if (cc_realm_kerberos_membership_call_join_finish (membership, result, &call_error)) {
                 g_debug ("Completed Join() method call");
                 return TRUE;
         }
@@ -574,7 +560,7 @@ cc_realm_join_finish (CcRealmObject *realm,
         dbus_error = g_dbus_error_get_remote_error (call_error);
         if (dbus_error == NULL) {
                 g_debug ("Join() failed because of %s", call_error->message);
-                g_propagate_error (error, call_error);
+                g_propagate_error (error, g_steal_pointer (&call_error));
                 return FALSE;
         }
 
@@ -584,13 +570,11 @@ cc_realm_join_finish (CcRealmObject *realm,
                 g_debug ("Join() failed because of invalid/insufficient credentials");
                 g_set_error (error, CC_REALM_ERROR, CC_REALM_ERROR_BAD_LOGIN,
                              "%s", call_error->message);
-                g_error_free (call_error);
         } else {
                 g_debug ("Join() failed because of %s", call_error->message);
-                g_propagate_error (error, call_error);
+                g_propagate_error (error, g_steal_pointer (&call_error));
         }
 
-        g_free (dbus_error);
         return FALSE;
 }
 
@@ -605,10 +589,10 @@ static void
 login_closure_free (gpointer data)
 {
         LoginClosure *login = data;
-        g_free (login->domain);
-        g_free (login->realm);
-        g_free (login->user);
-        g_free (login->password);
+        g_clear_pointer (&login->domain, g_free);
+        g_clear_pointer (&login->realm, g_free);
+        g_clear_pointer (&login->user, g_free);
+        g_clear_pointer (&login->password, g_free);
         g_slice_free (LoginClosure, login);
 }
 
@@ -624,7 +608,7 @@ login_perform_kinit (krb5_context k5,
         krb5_principal principal;
         krb5_ccache ccache;
         krb5_creds creds;
-        gchar *name;
+        g_autofree gchar *name = NULL;
 
         name = g_strdup_printf ("%s@%s", login, realm);
         code = krb5_parse_name (k5, name, &principal);
@@ -632,12 +616,10 @@ login_perform_kinit (krb5_context k5,
         if (code != 0) {
                 g_debug ("Couldn't parse principal name: %s: %s",
                          name, krb5_get_error_message (k5, code));
-                g_free (name);
                 return code;
         }
 
         g_debug ("Using principal name to kinit: %s", name);
-        g_free (name);
 
         if (filename == NULL)
                 code = krb5_cc_default (k5, &ccache);
@@ -677,16 +659,16 @@ login_perform_kinit (krb5_context k5,
 }
 
 static void
-kinit_thread_func (GTask *task,
+kinit_thread_func (GTask *t,
                    gpointer object,
                    gpointer task_data,
                    GCancellable *cancellable)
 {
+        g_autoptr(GTask) task = t;
         LoginClosure *login = task_data;
         krb5_context k5 = NULL;
         krb5_error_code code;
-        GError *error = NULL;
-        gchar *filename = NULL;
+        g_autofree gchar *filename = NULL;
         gchar *contents;
         gsize length;
         gint temp_fd;
@@ -697,8 +679,7 @@ kinit_thread_func (GTask *task,
         if (temp_fd == -1) {
                 g_warning ("Couldn't create credential cache file: %s: %s",
                            filename, g_strerror (errno));
-                g_free (filename);
-                filename = NULL;
+                g_clear_pointer (&filename, g_free);
         } else {
                 close (temp_fd);
         }
@@ -712,13 +693,13 @@ kinit_thread_func (GTask *task,
         switch (code) {
         case 0:
                 if (filename != NULL) {
-                        g_file_get_contents (filename, &contents, &length, &error);
-                        if (error == NULL) {
+                        g_autoptr(GError) error = NULL;
+
+                        if (g_file_get_contents (filename, &contents, &length, &error)) {
                                 g_debug ("Read in credential cache: %s", filename);
                         } else {
                                 g_warning ("Couldn't read credential cache: %s: %s",
                                            filename, error->message);
-                                g_error_free (error);
                         }
 
                         g_task_return_pointer (task, g_bytes_new_take (contents, length), (GDestroyNotify) 
g_bytes_unref);
@@ -755,13 +736,10 @@ kinit_thread_func (GTask *task,
         if (filename) {
                 g_unlink (filename);
                 g_debug ("Deleted credential cache: %s", filename);
-                g_free (filename);
         }
 
         if (k5)
                 krb5_free_context (k5);
-
-        g_object_unref (task);
 }
 
 void
@@ -774,7 +752,7 @@ cc_realm_login (CcRealmObject *realm,
 {
         GTask *task;
         LoginClosure *login;
-        CcRealmKerberos *kerberos;
+        g_autoptr(CcRealmKerberos) kerberos = NULL;
 
         g_return_if_fail (CC_REALM_IS_OBJECT (realm));
         g_return_if_fail (user != NULL);
@@ -796,8 +774,6 @@ cc_realm_login (CcRealmObject *realm,
 
         g_task_set_return_on_cancel (task, TRUE);
         g_task_run_in_thread (task, kinit_thread_func);
-
-        g_object_unref (kerberos);
 }
 
 GBytes *
diff --git a/panels/user-accounts/cc-user-image.c b/panels/user-accounts/cc-user-image.c
index 8dc6389aa..fe535edee 100644
--- a/panels/user-accounts/cc-user-image.c
+++ b/panels/user-accounts/cc-user-image.c
@@ -38,7 +38,8 @@ render_user_icon (ActUser *user,
                   gint     scale)
 {
         g_autoptr(GdkPixbuf) source_pixbuf = NULL;
-        GdkPixbuf    *pixbuf = NULL;
+        g_autoptr(GdkPixbuf) avatar_pixbuf = NULL;
+        g_autoptr(GdkPixbuf) pixbuf = NULL;
         const gchar  *icon_file;
         cairo_surface_t *surface = NULL;
 
@@ -46,7 +47,6 @@ render_user_icon (ActUser *user,
         g_return_val_if_fail (icon_size > 12, NULL);
 
         icon_file = act_user_get_icon_file (user);
-        pixbuf = NULL;
         if (icon_file) {
                 source_pixbuf = gdk_pixbuf_new_from_file_at_size (icon_file,
                                                                   icon_size * scale,
@@ -60,18 +60,13 @@ render_user_icon (ActUser *user,
                 goto out;
         }
 
-        if (source_pixbuf != NULL) {
-                g_object_unref (source_pixbuf);
-        }
-
-        source_pixbuf = generate_default_avatar (user, icon_size * scale);
-        if (source_pixbuf)
-            pixbuf = round_image (source_pixbuf);
+        avatar_pixbuf = generate_default_avatar (user, icon_size * scale);
+        if (avatar_pixbuf)
+            pixbuf = round_image (avatar_pixbuf);
  out:
 
         if (pixbuf != NULL) {
                 surface = gdk_cairo_surface_create_from_pixbuf (pixbuf, scale, NULL);
-                g_object_unref (pixbuf);
         }
 
         return surface;
diff --git a/panels/user-accounts/cc-user-panel.c b/panels/user-accounts/cc-user-panel.c
index a0c16c5e6..4c92422e0 100644
--- a/panels/user-accounts/cc-user-panel.c
+++ b/panels/user-accounts/cc-user-panel.c
@@ -132,12 +132,14 @@ typedef struct {
 static void
 async_delete_data_free (AsyncDeleteData *data)
 {
-        g_object_unref (data->self);
-        g_object_unref (data->cancellable);
-        g_free (data->login);
+        g_clear_object (&data->self);
+        g_clear_object (&data->cancellable);
+        g_clear_pointer (&data->login, g_free);
         g_slice_free (AsyncDeleteData, data);
 }
 
+G_DEFINE_AUTOPTR_CLEANUP_FUNC (AsyncDeleteData, async_delete_data_free)
+
 static void
 show_error_dialog (CcUserPanel *self,
                    const gchar *message,
@@ -199,7 +201,8 @@ static GtkWidget *
 create_carousel_entry (CcUserPanel *self, ActUser *user)
 {
         GtkWidget *box, *widget;
-        gchar *label;
+        g_autofree gchar *label = NULL;
+        g_autofree gchar *subtitle_label = NULL;
 
         box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
 
@@ -214,16 +217,14 @@ create_carousel_entry (CcUserPanel *self, ActUser *user)
         gtk_label_set_ellipsize (GTK_LABEL (widget), PANGO_ELLIPSIZE_END);
         gtk_widget_set_margin_top (widget, 5);
         gtk_box_pack_start (GTK_BOX (box), widget, FALSE, TRUE, 0);
-        g_free (label);
 
         if (act_user_get_uid (user) == getuid ())
-                label = g_strdup_printf ("<small>%s</small>", _("Your account"));
+                subtitle_label = g_strdup_printf ("<small>%s</small>", _("Your account"));
         else
-                label = g_strdup (" ");
+                subtitle_label = g_strdup (" ");
 
-        widget = gtk_label_new (label);
+        widget = gtk_label_new (subtitle_label);
         gtk_label_set_use_markup (GTK_LABEL (widget), TRUE);
-        g_free (label);
 
         gtk_box_pack_start (GTK_BOX (box), widget, FALSE, TRUE, 0);
         gtk_style_context_add_class (gtk_widget_get_style_context (widget),
@@ -266,30 +267,26 @@ static gint
 sort_users (gconstpointer a, gconstpointer b)
 {
         ActUser *ua, *ub;
-        gchar *name1, *name2;
-        gint result;
 
         ua = ACT_USER (a);
         ub = ACT_USER (b);
 
         /* Make sure the current user is shown first */
         if (act_user_get_uid (ua) == getuid ()) {
-                result = -G_MAXINT32;
+                return -G_MAXINT32;
         }
         else if (act_user_get_uid (ub) == getuid ()) {
-                result = G_MAXINT32;
+                return G_MAXINT32;
         }
         else {
+                g_autofree gchar *name1 = NULL;
+                g_autofree gchar *name2 = NULL;
+
                 name1 = g_utf8_collate_key (get_real_or_user_name (ua), -1);
                 name2 = g_utf8_collate_key (get_real_or_user_name (ub), -1);
 
-                result = strcmp (name1, name2);
-
-                g_free (name1);
-                g_free (name2);
+                return strcmp (name1, name2);
         }
-
-        return result;
 }
 
 static void
@@ -392,15 +389,12 @@ delete_user_done (ActUserManager *manager,
                   GAsyncResult   *res,
                   CcUserPanel    *self)
 {
-        GError *error;
+        g_autoptr(GError) error = NULL;
 
-        error = NULL;
         if (!act_user_manager_delete_user_finish (manager, res, &error)) {
                 if (!g_error_matches (error, ACT_USER_MANAGER_ERROR,
                                       ACT_USER_MANAGER_ERROR_PERMISSION_DENIED))
                         show_error_dialog (self, _("Failed to delete user"), error);
-
-                g_error_free (error);
         }
 }
 
@@ -444,33 +438,29 @@ enterprise_user_revoked (GObject *source,
                          GAsyncResult *result,
                          gpointer user_data)
 {
-        AsyncDeleteData *data = user_data;
+        g_autoptr(AsyncDeleteData) data = user_data;
         CcUserPanel *self = data->self;
         CcRealmCommon *common = CC_REALM_COMMON (source);
-        GError *error = NULL;
+        g_autoptr(GError) error = NULL;
 
         if (g_cancellable_is_cancelled (data->cancellable)) {
-                async_delete_data_free (data);
                 return;
         }
 
         cc_realm_common_call_change_login_policy_finish (common, result, &error);
         if (error != NULL) {
                 show_error_dialog (self, _("Failed to revoke remotely managed user"), error);
-                g_error_free (error);
         }
-
-        async_delete_data_free (data);
 }
 
 static CcRealmCommon *
 find_matching_realm (CcRealmManager *realm_manager, const gchar *login)
 {
         CcRealmCommon *common = NULL;
-        GList *realms, *l;
+        GList *realms;
 
         realms = cc_realm_manager_get_realms (realm_manager);
-        for (l = realms; l != NULL; l = g_list_next (l)) {
+        for (GList *l = realms; l != NULL; l = g_list_next (l)) {
                 const gchar * const *permitted_logins;
                 gint i;
 
@@ -499,25 +489,22 @@ realm_manager_found (GObject *source,
                      GAsyncResult *result,
                      gpointer user_data)
 {
-        AsyncDeleteData *data = user_data;
+        g_autoptr(AsyncDeleteData) data = user_data;
         CcUserPanel *self = data->self;
-        CcRealmCommon *common;
+        g_autoptr(CcRealmCommon) common = NULL;
         CcRealmManager *realm_manager;
         const gchar *add[1];
         const gchar *remove[2];
         GVariant *options;
-        GError *error = NULL;
+        g_autoptr(GError) error = NULL;
 
         if (g_cancellable_is_cancelled (data->cancellable)) {
-                async_delete_data_free (data);
                 return;
         }
 
         realm_manager = cc_realm_manager_new_finish (result, &error);
         if (error != NULL) {
                 show_error_dialog (self, _("Failed to revoke remotely managed user"), error);
-                g_error_free (error);
-                async_delete_data_free (data);
                 return;
         }
 
@@ -525,7 +512,6 @@ realm_manager_found (GObject *source,
         common = find_matching_realm (realm_manager, data->login);
         if (common == NULL) {
                 /* The realm was probably left */
-                async_delete_data_free (data);
                 return;
         }
 
@@ -541,9 +527,7 @@ realm_manager_found (GObject *source,
                                                   add, remove, options,
                                                   data->cancellable,
                                                   enterprise_user_revoked,
-                                                  data);
-
-        g_object_unref (common);
+                                                  g_steal_pointer (&data));
 }
 
 static void
@@ -551,25 +535,22 @@ enterprise_user_uncached (GObject           *source,
                           GAsyncResult      *res,
                           gpointer           user_data)
 {
-        AsyncDeleteData *data = user_data;
+        g_autoptr(AsyncDeleteData) data = user_data;
         CcUserPanel *self = data->self;
         ActUserManager *manager = ACT_USER_MANAGER (source);
-        GError *error = NULL;
+        g_autoptr(GError) error = NULL;
 
         if (g_cancellable_is_cancelled (data->cancellable)) {
-                async_delete_data_free (data);
                 return;
         }
 
         act_user_manager_uncache_user_finish (manager, res, &error);
         if (error == NULL) {
                 /* Find realm manager */
-                cc_realm_manager_new (cc_panel_get_cancellable (CC_PANEL (self)), realm_manager_found, data);
+                cc_realm_manager_new (cc_panel_get_cancellable (CC_PANEL (self)), realm_manager_found, 
g_steal_pointer (&data));
         }
         else {
                 show_error_dialog (self, _("Failed to revoke remotely managed user"), error);
-                g_error_free (error);
-                async_delete_data_free (data);
         }
 }
 
@@ -766,34 +747,31 @@ autologin_changed (CcUserPanel *self)
 static gchar *
 get_login_time_text (ActUser *user)
 {
-        gchar *text, *date_str, *time_str;
-        GDateTime *date_time;
         gint64 time;
 
         time = act_user_get_login_time (user);
         if (act_user_is_logged_in (user)) {
-                text = g_strdup (_("Logged in"));
+                return g_strdup (_("Logged in"));
         }
         else if (time > 0) {
+                g_autoptr(GDateTime) date_time = NULL;
+                g_autofree gchar *date_str = NULL;
+                g_autofree gchar *time_str = NULL;
+
                 date_time = g_date_time_new_from_unix_local (time);
                 date_str = cc_util_get_smart_date (date_time);
+
                 /* Translators: This is a time format string in the style of "22:58".
                    It indicates a login time which follows a date. */
                 time_str = g_date_time_format (date_time, C_("login date-time", "%k:%M"));
 
                 /* Translators: This indicates a login date-time.
                    The first %s is a date, and the second %s a time. */
-                text = g_strdup_printf(C_("login date-time", "%s, %s"), date_str, time_str);
-
-                g_date_time_unref (date_time);
-                g_free (date_str);
-                g_free (time_str);
+                return g_strdup_printf(C_("login date-time", "%s, %s"), date_str, time_str);
         }
         else {
-                text = g_strdup ("—");
+                return g_strdup ("—");
         }
-
-        return text;
 }
 
 static gboolean
@@ -868,7 +846,8 @@ update_fingerprint_row_state (CcUserPanel *self, GParamSpec *spec, CcFingerprint
 static void
 show_user (ActUser *user, CcUserPanel *self)
 {
-        gchar *lang, *text, *name;
+        g_autofree gchar *lang = NULL;
+        g_autofree gchar *name = NULL;
         gboolean show, enable;
         ActUser *current;
 
@@ -904,7 +883,6 @@ show_user (ActUser *user, CcUserPanel *self)
         g_signal_handlers_unblock_by_func (self->autologin_switch, autologin_changed, self);
         gtk_widget_set_sensitive (GTK_WIDGET (self->autologin_switch), get_autologin_possible (user));
 
-        name = NULL;
         lang = g_strdup (act_user_get_language (user));
 
         if (lang && *lang != '\0') {
@@ -914,8 +892,6 @@ show_user (ActUser *user, CcUserPanel *self)
         }
 
         gtk_label_set_label (self->language_button_label, name);
-        g_free (lang);
-        g_free (name);
 
         /* Fingerprint: show when self, local, enabled, and possible */
         show = (act_user_get_uid (user) == getuid() &&
@@ -969,9 +945,10 @@ show_user (ActUser *user, CcUserPanel *self)
         show = act_user_get_uid (user) == getuid () ||
                act_user_get_account_type (current) == ACT_USER_ACCOUNT_TYPE_ADMINISTRATOR;
         if (show) {
+                g_autofree gchar *text = NULL;
+
                 text = get_login_time_text (user);
                 gtk_label_set_label (self->last_login_button_label, text);
-                g_free (text);
         }
         gtk_widget_set_visible (GTK_WIDGET (self->last_login_row), show);
 
@@ -1059,7 +1036,7 @@ dismiss_notification (CcUserPanel *self)
 static void
 restart_now (CcUserPanel *self)
 {
-        GDBusConnection *bus;
+        g_autoptr(GDBusConnection) bus = NULL;
 
         gtk_revealer_set_reveal_child (self->notification_revealer, FALSE);
 
@@ -1072,7 +1049,6 @@ restart_now (CcUserPanel *self)
                                 g_variant_new ("(u)", 0),
                                 NULL, 0, G_MAXINT,
                                 NULL, NULL, NULL);
-        g_object_unref (bus);
 }
 
 static void
@@ -1292,7 +1268,7 @@ static void
 add_unlock_tooltip (GtkWidget *widget)
 {
         gchar *names[3];
-        GIcon *icon;
+        g_autoptr(GIcon) icon;
 
         names[0] = "changes-allow-symbolic";
         names[1] = "changes-allow";
@@ -1306,7 +1282,6 @@ add_unlock_tooltip (GtkWidget *widget)
                                           _("To make changes,\nclick the * icon first"),
                                           "*",
                                           icon);
-        g_object_unref (icon);
         g_signal_connect (widget, "button-release-event",
                            G_CALLBACK (show_tooltip_now), NULL);
 }
@@ -1381,7 +1356,7 @@ on_permission_changed (CcUserPanel *self)
         }
         else {
                 gchar *names[3];
-                GIcon *icon;
+                g_autoptr(GIcon) icon = NULL;
 
                 names[0] = "changes-allow-symbolic";
                 names[1] = "changes-allow";
@@ -1392,7 +1367,6 @@ on_permission_changed (CcUserPanel *self)
                                                   _("To delete the selected user account,\nclick the * icon 
first"),
                                                   "*",
                                                   icon);
-                g_object_unref (icon);
         }
 
         if (!act_user_is_local_account (user)) {
@@ -1486,8 +1460,8 @@ on_permission_changed (CcUserPanel *self)
 static void
 setup_main_window (CcUserPanel *self)
 {
-        GIcon *icon;
-        GError *error = NULL;
+        g_autoptr(GIcon) icon = NULL;
+        g_autoptr(GError) error = NULL;
         gchar *names[3];
         gboolean loaded;
 
@@ -1502,7 +1476,6 @@ setup_main_window (CcUserPanel *self)
                 on_permission_changed (self);
         } else {
                 g_warning ("Cannot create '%s' permission: %s", USER_ACCOUNTS_PERMISSION, error->message);
-                g_error_free (error);
         }
 
         names[0] = "changes-allow-symbolic";
@@ -1513,7 +1486,6 @@ setup_main_window (CcUserPanel *self)
                                           _("To delete the selected user account,\nclick the * icon first"),
                                           "*",
                                           icon);
-        g_object_unref (icon);
 
         g_object_get (self->um, "is-loaded", &loaded, NULL);
         if (loaded)
@@ -1571,7 +1543,7 @@ static void
 cc_user_panel_init (CcUserPanel *self)
 {
         volatile GType type G_GNUC_UNUSED;
-        GtkCssProvider *provider;
+        g_autoptr(GtkCssProvider) provider = NULL;
 
         g_resources_register (cc_user_accounts_get_resource ());
 
@@ -1589,7 +1561,6 @@ cc_user_panel_init (CcUserPanel *self)
         gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
                                                    GTK_STYLE_PROVIDER (provider),
                                                    GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
-        g_object_unref (provider);
 
         self->login_screen_settings = settings_or_null ("org.gnome.login-screen");
 
diff --git a/panels/user-accounts/run-passwd.c b/panels/user-accounts/run-passwd.c
index 56eea9f29..86f53d4fc 100644
--- a/panels/user-accounts/run-passwd.c
+++ b/panels/user-accounts/run-passwd.c
@@ -252,8 +252,6 @@ stop_passwd (PasswdHandler *passwd_handler)
 static void
 free_passwd_resources (PasswdHandler *passwd_handler)
 {
-        GError  *error = NULL;
-
         /* Remove the child watcher */
         if (passwd_handler->backend_child_watch_id != 0) {
 
@@ -265,28 +263,23 @@ free_passwd_resources (PasswdHandler *passwd_handler)
 
         /* Close IO channels (internal file descriptors are automatically closed) */
         if (passwd_handler->backend_stdin != NULL) {
+                g_autoptr(GError) error = NULL;
 
                 if (g_io_channel_shutdown (passwd_handler->backend_stdin, TRUE, &error) != 
G_IO_STATUS_NORMAL) {
                         g_warning ("Could not shutdown backend_stdin IO channel: %s", error->message);
-                        g_error_free (error);
-                        error = NULL;
                 }
 
-                g_io_channel_unref (passwd_handler->backend_stdin);
-                passwd_handler->backend_stdin = NULL;
+                g_clear_pointer (&passwd_handler->backend_stdin, g_io_channel_unref);
         }
 
         if (passwd_handler->backend_stdout != NULL) {
+                g_autoptr(GError) error = NULL;
 
                 if (g_io_channel_shutdown (passwd_handler->backend_stdout, TRUE, &error) != 
G_IO_STATUS_NORMAL) {
                         g_warning ("Could not shutdown backend_stdout IO channel: %s", error->message);
-                        g_error_free (error);
-                        error = NULL;
                 }
 
-                g_io_channel_unref (passwd_handler->backend_stdout);
-
-                passwd_handler->backend_stdout = NULL;
+                g_clear_pointer (&passwd_handler->backend_stdout, g_io_channel_unref);
         }
 
         /* Remove IO watcher */
@@ -321,9 +314,9 @@ free_passwd_resources (PasswdHandler *passwd_handler)
 static void
 io_queue_pop (GQueue *queue, GIOChannel *channel)
 {
-        gchar   *buf;
+        g_autofree gchar *buf = NULL;
         gsize   bytes_written;
-        GError  *error = NULL;
+        g_autoptr(GError) error = NULL;
 
         buf = g_queue_pop_head (queue);
 
@@ -331,12 +324,10 @@ io_queue_pop (GQueue *queue, GIOChannel *channel)
 
                 if (g_io_channel_write_chars (channel, buf, -1, &bytes_written, &error) != 
G_IO_STATUS_NORMAL) {
                         g_warning ("Could not write queue element \"%s\" to channel: %s", buf, 
error->message);
-                        g_error_free (error);
                 }
 
                 /* Ensure passwords are cleared from memory */
                 memset (buf, 0, strlen (buf));
-                g_free (buf);
         }
 }
 
@@ -377,8 +368,7 @@ io_watch_stdout (GIOChannel *source, GIOCondition condition, PasswdHandler *pass
 
         gchar           buf[BUFSIZE];           /* Temporary buffer */
         gsize           bytes_read;
-        GError          *gio_error = NULL;      /* Error returned by functions */
-        GError          *error = NULL;          /* Error sent to callbacks */
+        g_autoptr(GError) gio_error = NULL;
 
         gboolean        reinit = FALSE;
 
@@ -390,8 +380,6 @@ io_watch_stdout (GIOChannel *source, GIOCondition condition, PasswdHandler *pass
         if (g_io_channel_read_chars (source, buf, BUFSIZE, &bytes_read, &gio_error)
             != G_IO_STATUS_NORMAL) {
                 g_warning ("IO Channel read error: %s", gio_error->message);
-                g_error_free (gio_error);
-
                 return TRUE;
         }
 
@@ -419,6 +407,7 @@ io_watch_stdout (GIOChannel *source, GIOCondition condition, PasswdHandler *pass
 
                                 } else {
                                         /* Authentication failed */
+                                        g_autoptr(GError) error = NULL;
 
                                         error = g_error_new_literal (PASSWD_ERROR, PASSWD_ERROR_AUTH_FAILED,
                                                                      _("Authentication failed"));
@@ -435,8 +424,6 @@ io_watch_stdout (GIOChannel *source, GIOCondition condition, PasswdHandler *pass
                                                 passwd_handler->auth_cb (passwd_handler,
                                                                          error,
                                                                          passwd_handler->auth_cb_data);
-
-                                        g_error_free (error);
                                 }
 
                                 reinit = TRUE;
@@ -492,6 +479,7 @@ io_watch_stdout (GIOChannel *source, GIOCondition condition, PasswdHandler *pass
                                 }
                                 else {
                                         /* Ohnoes! */
+                                        g_autoptr(GError) error = NULL;
 
                                         if (strstr (str->str, "recovered") != NULL) {
                                                 /* What does this indicate?
@@ -551,9 +539,6 @@ io_watch_stdout (GIOChannel *source, GIOCondition condition, PasswdHandler *pass
                                                 passwd_handler->chpasswd_cb (passwd_handler,
                                                                              error,
                                                                              
passwd_handler->chpasswd_cb_data);
-
-                                        g_error_free (error);
-
                                 }
 
                                 reinit = TRUE;
@@ -569,14 +554,13 @@ io_watch_stdout (GIOChannel *source, GIOCondition condition, PasswdHandler *pass
                                  * passwd will immediately ask for the new password,
                                  * so skip the AUTH phase */
                                 if (is_string_complete (str->str, "new", "New", NULL)) {
-                                        gchar *pw;
+                                        g_autofree gchar *pw = NULL;
 
                                         passwd_handler->backend_state = PASSWD_STATE_NEW;
 
                                         /* since passwd didn't ask for our old password
                                          * in this case, simply remove it from the queue */
                                         pw = g_queue_pop_head (passwd_handler->backend_stdin_queue);
-                                        g_free (pw);
 
                                         /* Pop the IO queue, i.e. send new password */
                                         io_queue_pop (passwd_handler->backend_stdin_queue, 
passwd_handler->backend_stdin);
@@ -679,7 +663,7 @@ passwd_authenticate (PasswdHandler *passwd_handler,
                      PasswdCallback cb,
                      const gpointer user_data)
 {
-        GError *error = NULL;
+        g_autoptr(GError) error = NULL;
 
         /* Don't stop if we've already started changing password */
         if (passwd_handler->changing_password)
@@ -701,8 +685,6 @@ passwd_authenticate (PasswdHandler *passwd_handler,
 
         if (!spawn_passwd (passwd_handler, &error)) {
                 g_warning ("%s", error->message);
-                g_error_free (error);
-
                 return;
         }
 
@@ -717,8 +699,6 @@ passwd_change_password (PasswdHandler *passwd_handler,
                         PasswdCallback cb,
                         const gpointer user_data)
 {
-        GError *error = NULL;
-
         passwd_handler->changing_password = TRUE;
 
         passwd_handler->new_password = new_password;
@@ -736,14 +716,13 @@ passwd_change_password (PasswdHandler *passwd_handler,
          * has occurred but it has not yet exited */
         if (passwd_handler->backend_pid == -1) {
                 /* If it is not, re-run authentication */
+                g_autoptr(GError) error = NULL;
 
                 /* Spawn backend */
                 stop_passwd (passwd_handler);
 
                 if (!spawn_passwd (passwd_handler, &error)) {
                         g_warning ("%s", error->message);
-                        g_error_free (error);
-
                         return FALSE;
                 }
 
diff --git a/panels/user-accounts/user-utils.c b/panels/user-accounts/user-utils.c
index 3c17dfedd..eb818b666 100644
--- a/panels/user-accounts/user-utils.c
+++ b/panels/user-accounts/user-utils.c
@@ -72,9 +72,9 @@ icon_shape_data_free (gpointer user_data)
 {
         IconShapeData *data = user_data;
 
-        g_free (data->text);
-        g_free (data->placeholder_str);
-        g_object_unref (data->icon);
+        g_clear_pointer (&data->text, g_free);
+        g_clear_pointer (&data->placeholder_str, g_free);
+        g_clear_object (&data->icon);
         g_free (data);
 }
 
@@ -91,8 +91,8 @@ icon_shape_renderer (cairo_t        *cr,
         if (GPOINTER_TO_UINT (attr->data) == data->placeholder) {
                 gdouble ascent;
                 gdouble height;
-                GdkPixbuf *pixbuf;
-                GtkIconInfo *info;
+                g_autoptr(GdkPixbuf) pixbuf = NULL;
+                g_autoptr(GtkIconInfo) info = NULL;
 
                 ascent = pango_units_to_double (attr->ink_rect.y);
                 height = pango_units_to_double (attr->ink_rect.height);
@@ -101,13 +101,11 @@ icon_shape_renderer (cairo_t        *cr,
                                                        (gint)height,
                                                        GTK_ICON_LOOKUP_FORCE_SIZE | 
GTK_ICON_LOOKUP_USE_BUILTIN);
                 pixbuf = gtk_icon_info_load_icon (info, NULL);
-                g_object_unref (info);
 
                 cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
                 cairo_reset_clip (cr);
                 gdk_cairo_set_source_pixbuf (cr, pixbuf, x, y + ascent);
                 cairo_paint (cr);
-                g_object_unref (pixbuf);
         }
 }
 
@@ -251,13 +249,12 @@ query_tooltip (GtkWidget  *widget,
                GtkTooltip *tooltip,
                gpointer    user_data)
 {
-        gchar *tip;
-
         if (GTK_ENTRY_ICON_SECONDARY == gtk_entry_get_icon_at_pos (GTK_ENTRY (widget), x, y)) {
+                g_autofree gchar *tip = NULL;
+
                 tip = gtk_entry_get_icon_tooltip_text (GTK_ENTRY (widget),
                                                        GTK_ENTRY_ICON_SECONDARY);
                 gtk_tooltip_set_text (tooltip, tip);
-                g_free (tip);
 
                 return TRUE;
         }
@@ -398,8 +395,8 @@ typedef struct {
 static void
 is_valid_username_data_free (isValidUsernameData *data)
 {
-        g_free (data->username);
-        g_free (data->tip);
+        g_clear_pointer (&data->username, g_free);
+        g_clear_pointer (&data->tip, g_free);
         g_free (data);
 }
 
@@ -420,7 +417,7 @@ is_valid_username_child_watch_cb (GPid pid,
                                   gint status,
                                   gpointer user_data)
 {
-        GTask *task = G_TASK (user_data);
+        g_autoptr(GTask) task = G_TASK (user_data);
         isValidUsernameData *data = g_task_get_task_data (task);
         GError *error = NULL;
         gboolean valid = FALSE;
@@ -452,7 +449,6 @@ is_valid_username_child_watch_cb (GPid pid,
         }
 
         g_spawn_close_pid (pid);
-        g_object_unref (task);
 }
 
 void
@@ -461,7 +457,7 @@ is_valid_username_async (const gchar *username,
                          GAsyncReadyCallback callback,
                          gpointer callback_data)
 {
-        GTask *task;
+        g_autoptr(GTask) task = NULL;
         isValidUsernameData *data;
         gchar *argv[6];
         GPid pid;
@@ -476,15 +472,11 @@ is_valid_username_async (const gchar *username,
 
         if (username == NULL || username[0] == '\0') {
                 g_task_return_boolean (task, FALSE);
-                g_object_unref (task);
-
                 return;
         }
         else if (strlen (username) > get_username_max_length ()) {
                 data->tip = g_strdup (_("The username is too long."));
                 g_task_return_boolean (task, FALSE);
-                g_object_unref (task);
-
                 return;
         }
 
@@ -518,12 +510,11 @@ is_valid_username_async (const gchar *username,
                             G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
                             NULL, NULL, &pid, &error)) {
                 g_task_return_error (task, error);
-                g_object_unref (task);
-
                 return;
         }
 
         g_child_watch_add (pid, (GChildWatchFunc) is_valid_username_child_watch_cb, task);
+        g_steal_pointer (&task);
 }
 
 gboolean
@@ -707,40 +698,32 @@ void
 set_user_icon_data (ActUser   *user,
                     GdkPixbuf *pixbuf)
 {
-        gchar *path;
+        g_autofree gchar *path = NULL;
         gint fd;
-        GOutputStream *stream;
-        GError *error;
+        g_autoptr(GOutputStream) stream = NULL;
+        g_autoptr(GError) error = NULL;
 
         path = g_build_filename (g_get_tmp_dir (), "gnome-control-center-user-icon-XXXXXX", NULL);
         fd = g_mkstemp (path);
 
         if (fd == -1) {
                 g_warning ("failed to create temporary file for image data");
-                g_free (path);
                 return;
         }
 
         stream = g_unix_output_stream_new (fd, TRUE);
 
-        error = NULL;
         if (!gdk_pixbuf_save_to_stream (pixbuf, stream, "png", NULL, &error, NULL)) {
                 g_warning ("failed to save image: %s", error->message);
-                g_error_free (error);
-                g_object_unref (stream);
                 return;
         }
 
-        g_object_unref (stream);
-
         act_user_set_icon_file (user, path);
 
         /* if we ever make the dbus call async, the g_remove call needs
          * to wait for its completion
          */
         g_remove (path);
-
-        g_free (path);
 }
 
 GdkPixbuf *


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