[gnome-software/gnome-3-10] Fix a few small memory leaks



commit 7ac3413223ac13366424f641218cc235bc7575f8
Author: Richard Hughes <richard hughsie com>
Date:   Thu Sep 26 13:57:30 2013 +0100

    Fix a few small memory leaks

 src/gs-app.c                               |    1 +
 src/gs-category.c                          |   12 +++++++-
 src/gs-plugin-loader.c                     |   32 ++++++++++++++++-----
 src/gs-plugin.c                            |   20 ++++++++++++-
 src/gs-plugin.h                            |    2 +
 src/gs-self-test.c                         |   11 ++++---
 src/gs-shell-category.c                    |    2 +-
 src/gs-shell-details.c                     |    4 ++
 src/gs-shell-installed.c                   |    6 +++-
 src/gs-shell-overview.c                    |    6 ++--
 src/gs-shell-search.c                      |    3 ++
 src/gs-shell-updates.c                     |    2 +-
 src/gs-shell.c                             |    3 ++
 src/plugins/gs-plugin-appstream.c          |    1 +
 src/plugins/gs-plugin-datadir-filename.c   |    6 ++--
 src/plugins/gs-plugin-hardcoded-featured.c |   42 +++++++++++----------------
 src/plugins/gs-plugin-hardcoded-popular.c  |    1 +
 src/plugins/gs-plugin-packagekit.c         |    1 +
 18 files changed, 106 insertions(+), 49 deletions(-)
---
diff --git a/src/gs-app.c b/src/gs-app.c
index e004658..6c08836 100644
--- a/src/gs-app.c
+++ b/src/gs-app.c
@@ -1041,6 +1041,7 @@ gs_app_finalize (GObject *object)
 
        g_free (priv->id);
        g_free (priv->name);
+       g_free (priv->url);
        g_free (priv->source);
        g_free (priv->version);
        g_free (priv->summary);
diff --git a/src/gs-category.c b/src/gs-category.c
index 306fe2d..c1c7285 100644
--- a/src/gs-category.c
+++ b/src/gs-category.c
@@ -97,6 +97,11 @@ gs_category_get_parent (GsCategory *category)
        return category->priv->parent;
 }
 
+/**
+ * gs_category_get_subcategories:
+ *
+ * Return value: (element-type GsApp) (transfer container): A list of subcategories
+ **/
 GList *
 gs_category_get_subcategories (GsCategory *category)
 {
@@ -104,11 +109,16 @@ gs_category_get_subcategories (GsCategory *category)
        return g_list_copy (category->priv->subcategories);
 }
 
+/**
+ * gs_category_add_subcategory:
+ *
+ * DANGER WILL ROBINSON! @subcategory is not ref'd
+ **/
 void
 gs_category_add_subcategory (GsCategory *category, GsCategory *subcategory)
 {
        g_return_if_fail (GS_IS_CATEGORY (category));
-       category->priv->subcategories = g_list_prepend (category->priv->subcategories, g_object_ref 
(subcategory));
+       category->priv->subcategories = g_list_prepend (category->priv->subcategories, subcategory);
 }
 
 /**
diff --git a/src/gs-plugin-loader.c b/src/gs-plugin-loader.c
index 8c83677..349ba25 100644
--- a/src/gs-plugin-loader.c
+++ b/src/gs-plugin-loader.c
@@ -155,7 +155,7 @@ gs_plugin_loader_list_uniq (GsPluginLoader *plugin_loader, GList *list)
                g_debug ("ignoring duplicate %s", id);
        }
 
-       g_list_free_full (list, (GDestroyNotify) g_object_unref);
+       gs_plugin_list_free (list);
        g_hash_table_unref (hash);
        return list_new;
 }
@@ -293,7 +293,7 @@ gs_plugin_loader_run_results (GsPluginLoader *plugin_loader,
        }
 out:
        if (!ret) {
-               g_list_free_full (list, (GDestroyNotify) g_object_unref);
+               gs_plugin_list_free (list);
                list = NULL;
        }
        return list;
@@ -434,9 +434,11 @@ cd_plugin_loader_get_all_state_finish (GsPluginLoaderAsyncState *state,
                                       const GError *error)
 {
        if (state->ret) {
+               GList *list;
+               list = g_list_copy_deep (state->list, (GCopyFunc) g_object_ref, NULL);
                g_simple_async_result_set_op_res_gpointer (state->res,
-                                                          g_list_copy (state->list),
-                                                          (GDestroyNotify) g_list_free);
+                                                          list,
+                                                          (GDestroyNotify) gs_plugin_list_free);
        } else {
                g_simple_async_result_set_from_error (state->res, error);
        }
@@ -446,7 +448,7 @@ cd_plugin_loader_get_all_state_finish (GsPluginLoaderAsyncState *state,
                g_object_unref (state->cancellable);
 
        g_free (state->value);
-       g_list_free (state->list);
+       gs_plugin_list_free (state->list);
        g_object_unref (state->res);
        g_object_unref (state->plugin_loader);
        g_slice_free (GsPluginLoaderAsyncState, state);
@@ -630,6 +632,8 @@ gs_plugin_loader_get_updates_async (GsPluginLoader *plugin_loader,
 
 /**
  * gs_plugin_loader_get_updates_finish:
+ *
+ * Return value: (element-type GsApp) (transfer full): A list of applications
  **/
 GList *
 gs_plugin_loader_get_updates_finish (GsPluginLoader *plugin_loader,
@@ -750,6 +754,8 @@ gs_plugin_loader_get_installed_async (GsPluginLoader *plugin_loader,
 
 /**
  * gs_plugin_loader_get_installed_finish:
+ *
+ * Return value: (element-type GsApp) (transfer full): A list of applications
  **/
 GList *
 gs_plugin_loader_get_installed_finish (GsPluginLoader *plugin_loader,
@@ -852,6 +858,8 @@ gs_plugin_loader_get_popular_async (GsPluginLoader *plugin_loader,
 
 /**
  * gs_plugin_loader_get_popular_finish:
+ *
+ * Return value: (element-type GsApp) (transfer full): A list of applications
  **/
 GList *
 gs_plugin_loader_get_popular_finish (GsPluginLoader *plugin_loader,
@@ -972,6 +980,8 @@ gs_plugin_loader_get_featured_async (GsPluginLoader *plugin_loader,
 
 /**
  * gs_plugin_loader_get_featured_finish:
+ *
+ * Return value: (element-type GsApp) (transfer full): A list of applications
  **/
 GList *
 gs_plugin_loader_get_featured_finish (GsPluginLoader *plugin_loader,
@@ -1139,6 +1149,8 @@ gs_plugin_loader_search_async (GsPluginLoader *plugin_loader,
 
 /**
  * gs_plugin_loader_search_finish:
+ *
+ * Return value: (element-type GsApp) (transfer full): A list of applications
  **/
 GList *
 gs_plugin_loader_search_finish (GsPluginLoader *plugin_loader,
@@ -1157,7 +1169,7 @@ gs_plugin_loader_search_finish (GsPluginLoader *plugin_loader,
                return NULL;
 
        /* grab detail */
-       return g_list_copy (g_simple_async_result_get_op_res_gpointer (simple));
+       return gs_plugin_list_copy (g_simple_async_result_get_op_res_gpointer (simple));
 }
 
 /******************************************************************************/
@@ -1287,6 +1299,8 @@ gs_plugin_loader_get_categories_async (GsPluginLoader *plugin_loader,
 
 /**
  * gs_plugin_loader_get_categories_finish:
+ *
+ * Return value: (element-type GsCategory) (transfer full): A list of applications
  **/
 GList *
 gs_plugin_loader_get_categories_finish (GsPluginLoader *plugin_loader,
@@ -1459,6 +1473,8 @@ gs_plugin_loader_get_category_apps_async (GsPluginLoader *plugin_loader,
 
 /**
  * gs_plugin_loader_get_category_apps_finish:
+ *
+ * Return value: (element-type GsApp) (transfer full): A list of applications
  **/
 GList *
 gs_plugin_loader_get_category_apps_finish (GsPluginLoader *plugin_loader,
@@ -1477,7 +1493,7 @@ gs_plugin_loader_get_category_apps_finish (GsPluginLoader *plugin_loader,
                return NULL;
 
        /* grab detail */
-       return g_list_copy (g_simple_async_result_get_op_res_gpointer (simple));
+       return gs_plugin_list_copy (g_simple_async_result_get_op_res_gpointer (simple));
 }
 
 /******************************************************************************/
@@ -1785,7 +1801,7 @@ gs_plugin_loader_app_refine (GsPluginLoader *plugin_loader,
                                           error);
        if (!ret)
                goto out;
-       g_list_free (list);
+       gs_plugin_list_free (list);
 out:
        return ret;
 }
diff --git a/src/gs-plugin.c b/src/gs-plugin.c
index fa2e9ac..588f4fb 100644
--- a/src/gs-plugin.c
+++ b/src/gs-plugin.c
@@ -50,7 +50,25 @@ gs_plugin_status_to_string (GsPluginStatus status)
 void
 gs_plugin_add_app (GList **list, GsApp *app)
 {
-       *list = g_list_prepend (*list, app);
+       *list = g_list_prepend (*list, g_object_ref (app));
+}
+
+/**
+ * gs_plugin_list_free:
+ **/
+void
+gs_plugin_list_free (GList *list)
+{
+       g_list_free_full (list, (GDestroyNotify) g_object_unref);
+}
+
+/**
+ * gs_plugin_list_copy:
+ **/
+GList *
+gs_plugin_list_copy (GList *list)
+{
+       return g_list_copy_deep (list, (GCopyFunc) g_object_ref, NULL);
 }
 
 typedef struct {
diff --git a/src/gs-plugin.h b/src/gs-plugin.h
index 598d061..beac05e 100644
--- a/src/gs-plugin.h
+++ b/src/gs-plugin.h
@@ -104,6 +104,8 @@ void                 gs_plugin_initialize                   (GsPlugin       *plugin);
 void            gs_plugin_destroy                      (GsPlugin       *plugin);
 void            gs_plugin_add_app                      (GList          **list,
                                                         GsApp          *app);
+void            gs_plugin_list_free                    (GList          *list);
+GList          *gs_plugin_list_copy                    (GList          *list);
 void            gs_plugin_status_update                (GsPlugin       *plugin,
                                                         GsApp          *app,
                                                         GsPluginStatus  status);
diff --git a/src/gs-self-test.c b/src/gs-self-test.c
index ad67f1e..9a7817b 100644
--- a/src/gs-self-test.c
+++ b/src/gs-self-test.c
@@ -130,7 +130,7 @@ gs_plugin_loader_func (void)
        g_assert_cmpstr (gs_app_get_id (app), ==, "gedit");
        g_assert_cmpstr (gs_app_get_summary (app), ==, "Edit text files");
 
-       g_list_free_full (list, (GDestroyNotify) g_object_unref);
+       gs_plugin_list_free (list);
 
        /* get updates */
        _status_changed_cnt = 0;
@@ -150,7 +150,7 @@ gs_plugin_loader_func (void)
        g_assert_cmpstr (gs_app_get_name (app), ==, "Boxes");
        g_assert_cmpstr (gs_app_get_summary (app), ==, "Do not segfault when using newer versons of 
libvirt.");
        g_assert_cmpint (gs_app_get_kind (app), ==, GS_APP_KIND_NORMAL);
-       g_list_free_full (list, (GDestroyNotify) g_object_unref);
+       gs_plugin_list_free (list);
 
        /* test packagekit */
        gs_plugin_loader_set_enabled (loader, "dummy", FALSE);
@@ -178,14 +178,14 @@ gs_plugin_loader_func (void)
        g_assert_cmpint (gs_app_get_state (app), ==, GS_APP_STATE_INSTALLED);
        g_assert_cmpint (gs_app_get_kind (app), ==, GS_APP_KIND_SYSTEM);
        g_assert (gs_app_get_pixbuf (app) != NULL);
-       g_list_free_full (list, (GDestroyNotify) g_object_unref);
+       gs_plugin_list_free (list);
 
        /* do this again, which should be much faster */
        list = gs_plugin_loader_get_installed (loader, GS_PLUGIN_LOADER_FLAGS_NONE, NULL, &error);
        g_assert_no_error (error);
        g_assert (list != NULL);
        g_assert_cmpint (g_list_length (list), >, 50);
-       g_list_free_full (list, (GDestroyNotify) g_object_unref);
+       gs_plugin_list_free (list);
 
        /* set a rating */
        gs_plugin_loader_set_enabled (loader, "packagekit", FALSE);
@@ -299,10 +299,11 @@ gs_plugin_loader_empty_func (void)
                        }
                        g_list_free_full (apps, (GDestroyNotify) g_object_unref);
                }
+               g_list_free (subcats);
        }
        g_assert_cmpint (empty_subcats_cnt, ==, 0);
 
-       g_list_free_full (list, (GDestroyNotify) g_object_unref);
+       gs_plugin_list_free (list);
        g_object_unref (loader);
 }
 
diff --git a/src/gs-shell-category.c b/src/gs-shell-category.c
index 0271328..846f88e 100644
--- a/src/gs-shell-category.c
+++ b/src/gs-shell-category.c
@@ -113,7 +113,7 @@ gs_shell_category_get_apps_cb (GObject *source_object,
                gtk_grid_attach (GTK_GRID (grid), priv->col1_placeholder, 1, 0, 1, 1);
 
 out:
-       g_list_free (list);
+       gs_plugin_list_free (list);
 
 }
 
diff --git a/src/gs-shell-details.c b/src/gs-shell-details.c
index 89dcb6c..888994a 100644
--- a/src/gs-shell-details.c
+++ b/src/gs-shell-details.c
@@ -605,6 +605,10 @@ gs_shell_details_finalize (GObject *object)
        GsShellDetails *shell_details = GS_SHELL_DETAILS (object);
        GsShellDetailsPrivate *priv = shell_details->priv;
 
+       g_object_unref (priv->history_sizegroup_state);
+       g_object_unref (priv->history_sizegroup_timestamp);
+       g_object_unref (priv->history_sizegroup_version);
+
        g_object_unref (priv->builder);
        g_object_unref (priv->plugin_loader);
        g_object_unref (priv->cancellable);
diff --git a/src/gs-shell-installed.c b/src/gs-shell-installed.c
index 6dbb820..7edd23f 100644
--- a/src/gs-shell-installed.c
+++ b/src/gs-shell-installed.c
@@ -243,7 +243,8 @@ gs_shell_installed_get_installed_cb (GObject *source_object,
                app = GS_APP (l->data);
                gs_shell_installed_add_app (shell_installed, app);
        }
-out: ;
+out:
+       gs_plugin_list_free (list);
 }
 
 static void
@@ -487,6 +488,9 @@ gs_shell_installed_finalize (GObject *object)
        GsShellInstalled *shell_installed = GS_SHELL_INSTALLED (object);
        GsShellInstalledPrivate *priv = shell_installed->priv;
 
+       g_object_unref (priv->sizegroup_image);
+       g_object_unref (priv->sizegroup_name);
+
        g_object_unref (priv->builder);
        g_object_unref (priv->plugin_loader);
        g_object_unref (priv->cancellable);
diff --git a/src/gs-shell-overview.c b/src/gs-shell-overview.c
index 6c8612a..6b60ec0 100644
--- a/src/gs-shell-overview.c
+++ b/src/gs-shell-overview.c
@@ -113,6 +113,7 @@ gs_shell_overview_get_popular_cb (GObject *source_object,
        priv->empty = FALSE;
 
 out:
+       gs_plugin_list_free (list);
        priv->refresh_count--;
        if (priv->refresh_count == 0)
                g_signal_emit (shell, signals[SIGNAL_REFRESHED], 0);
@@ -164,8 +165,7 @@ gs_shell_overview_get_featured_cb (GObject *source_object,
        priv->empty = FALSE;
 
 out:
-       g_list_free (list);
-
+       gs_plugin_list_free (list);
        priv->refresh_count--;
        if (priv->refresh_count == 0)
                g_signal_emit (shell, signals[SIGNAL_REFRESHED], 0);
@@ -221,8 +221,8 @@ gs_shell_overview_get_categories_cb (GObject *source_object,
                i++;
                has_category = TRUE;
        }
-       g_list_free_full (list, g_object_unref);
 out:
+       gs_plugin_list_free (list);
        if (has_category) {
                priv->empty = FALSE;
        }
diff --git a/src/gs-shell-search.c b/src/gs-shell-search.c
index 4b25639..cef6f26 100644
--- a/src/gs-shell-search.c
+++ b/src/gs-shell-search.c
@@ -383,6 +383,9 @@ gs_shell_search_finalize (GObject *object)
        GsShellSearch *shell_search = GS_SHELL_SEARCH (object);
        GsShellSearchPrivate *priv = shell_search->priv;
 
+       g_object_unref (priv->sizegroup_image);
+       g_object_unref (priv->sizegroup_name);
+
        g_object_unref (priv->builder);
        g_object_unref (priv->plugin_loader);
        g_object_unref (priv->cancellable);
diff --git a/src/gs-shell-updates.c b/src/gs-shell-updates.c
index 18ef3c5..64c48d2 100644
--- a/src/gs-shell-updates.c
+++ b/src/gs-shell-updates.c
@@ -139,7 +139,7 @@ gs_shell_updates_get_updates_cb (GsPluginLoader *plugin_loader,
 
 out:
        if (list != NULL)
-               g_list_free_full (list, (GDestroyNotify) g_object_unref);
+               gs_plugin_list_free (list);
 }
 
 /**
diff --git a/src/gs-shell.c b/src/gs-shell.c
index 287985e..b208820 100644
--- a/src/gs-shell.c
+++ b/src/gs-shell.c
@@ -199,10 +199,12 @@ save_back_entry (GsShell *shell)
        entry->mode = priv->mode;
 
        if (priv->mode == GS_SHELL_MODE_CATEGORY) {
+               g_clear_object (&entry->category);
                entry->category = gs_shell_category_get_category (priv->shell_category);
                g_object_ref (entry->category);
        }
        else if (priv->mode == GS_SHELL_MODE_DETAILS) {
+               g_clear_object (&entry->app);
                entry->app = gs_shell_details_get_app (priv->shell_details);
                g_object_ref (entry->app);
        }
@@ -614,6 +616,7 @@ gs_shell_finalize (GObject *object)
        GsShell *shell = GS_SHELL (object);
        GsShellPrivate *priv = shell->priv;
 
+       g_slist_free_full (priv->back_entry_stack, (GDestroyNotify) free_back_entry);
        g_object_unref (priv->builder);
        g_object_unref (priv->cancellable);
        g_object_unref (priv->plugin_loader);
diff --git a/src/plugins/gs-plugin-appstream.c b/src/plugins/gs-plugin-appstream.c
index 4426d58..9114553 100644
--- a/src/plugins/gs-plugin-appstream.c
+++ b/src/plugins/gs-plugin-appstream.c
@@ -676,6 +676,7 @@ gs_plugin_add_categories (GsPlugin *plugin,
                                gs_category_increment_size (parent);
                        }
                }
+               g_list_free (children);
        }
 out:
        return ret;
diff --git a/src/plugins/gs-plugin-datadir-filename.c b/src/plugins/gs-plugin-datadir-filename.c
index 43b14a8..a58e6ed 100644
--- a/src/plugins/gs-plugin-datadir-filename.c
+++ b/src/plugins/gs-plugin-datadir-filename.c
@@ -81,7 +81,7 @@ gs_plugin_datadir_filename_find (GsPlugin *plugin,
        const gchar *id;
        gchar *path_tmp = NULL;
        gboolean ret;
-       gchar *path = NULL;
+       gchar *path;
        const char * const *datadirs;
        int i;
 
@@ -109,7 +109,7 @@ gs_plugin_datadir_filename_find (GsPlugin *plugin,
                path = g_strdup_printf ("%s/applications/%s.desktop",
                                        datadirs[i], gs_app_get_id (app));
                if (g_file_test (path, G_FILE_TEST_EXISTS)) {
-                       path_tmp = g_strdup (path);
+                       path_tmp = path;
                        g_mutex_lock (&plugin->priv->plugin_mutex);
                        g_hash_table_insert (plugin->priv->cache,
                                             g_strdup (id),
@@ -117,6 +117,7 @@ gs_plugin_datadir_filename_find (GsPlugin *plugin,
                        g_mutex_unlock (&plugin->priv->plugin_mutex);
                        break;
                }
+               g_free (path);
        }
 
        if (path_tmp == NULL) {
@@ -128,7 +129,6 @@ gs_plugin_datadir_filename_find (GsPlugin *plugin,
                g_mutex_unlock (&plugin->priv->plugin_mutex);
        }
 out:
-       g_free (path);
        return path_tmp;
 }
 
diff --git a/src/plugins/gs-plugin-hardcoded-featured.c b/src/plugins/gs-plugin-hardcoded-featured.c
index 042d572..dad80bc 100644
--- a/src/plugins/gs-plugin-hardcoded-featured.c
+++ b/src/plugins/gs-plugin-hardcoded-featured.c
@@ -50,34 +50,24 @@ gs_plugin_add_featured (GsPlugin *plugin,
                        GCancellable *cancellable,
                        GError **error)
 {
-       gboolean ret = TRUE;
-       gchar *path;
-       GsApp *app;
-       guint i;
        GDateTime *date;
-       gchar **apps;
-       gsize n_apps;
-       GError *local_error = NULL;
        GKeyFile *kf;
+       GsApp *app;
+       const gchar *group = NULL;
+       gboolean ret = TRUE;
+       gchar **apps = NULL;
+       gchar *path;
        gchar *s;
-       const gchar *group;
-
-       apps = NULL;
+       gsize n_apps;
+       guint i;
 
        path = g_build_filename (DATADIR, "gnome-software", "featured.ini", NULL);
        kf = g_key_file_new ();
-       if (!g_key_file_load_from_file (kf, path, 0, &local_error)) {
-               g_warning ("Failed to read %s: %s", path, local_error->message);
-               ret = FALSE;
+       ret = g_key_file_load_from_file (kf, path, 0, error);
+       if (!ret)
                goto out;
-       }
-       g_free (path);
-
-       apps = g_key_file_get_groups (kf, &n_apps)
-;
-
-       group = NULL;
 
+       apps = g_key_file_get_groups (kf, &n_apps);
        if (g_getenv ("GNOME_SOFTWARE_FEATURED")) {
                const gchar *featured;
                featured = g_getenv ("GNOME_SOFTWARE_FEATURED");
@@ -103,24 +93,26 @@ gs_plugin_add_featured (GsPlugin *plugin,
 
        app = gs_app_new (group);
        s = g_key_file_get_string (kf, group, "background", NULL);
-       if (s) {
+       if (s != NULL) {
                gs_app_set_metadata (app, "Featured::background", s);
                g_free (s);
        }
        s = g_key_file_get_string (kf, group, "stroke", NULL);
-       if (s) {
+       if (s != NULL) {
                gs_app_set_metadata (app, "Featured::stroke-color", s);
                g_free (s);
        }
        s = g_key_file_get_string (kf, group, "text", NULL);
-       if (s) {
+       if (s != NULL) {
                gs_app_set_metadata (app, "Featured::text-color", s);
                g_free (s);
        }
        gs_plugin_add_app (list, app);
-
+       g_object_unref (app);
 out:
+       if (kf != NULL)
+               g_key_file_unref (kf);
+       g_free (path);
        g_strfreev (apps);
-
        return ret;
 }
diff --git a/src/plugins/gs-plugin-hardcoded-popular.c b/src/plugins/gs-plugin-hardcoded-popular.c
index 6ae5178..b4f0a1b 100644
--- a/src/plugins/gs-plugin-hardcoded-popular.c
+++ b/src/plugins/gs-plugin-hardcoded-popular.c
@@ -92,6 +92,7 @@ gs_plugin_add_popular (GsPlugin *plugin,
                for (i = 0; popular[i]; i++) {
                        app = gs_app_new (popular[i]);
                        gs_plugin_add_app (list, app);
+                       g_object_unref (app);
                }
 
                g_strfreev (popular);
diff --git a/src/plugins/gs-plugin-packagekit.c b/src/plugins/gs-plugin-packagekit.c
index 36019e0..c3dd60e 100644
--- a/src/plugins/gs-plugin-packagekit.c
+++ b/src/plugins/gs-plugin-packagekit.c
@@ -182,6 +182,7 @@ gs_plugin_packagekit_add_installed_results (GsPlugin *plugin,
                }
                gs_app_set_kind (app, GS_APP_KIND_PACKAGE);
                gs_plugin_add_app (list, app);
+               g_object_unref (app);
        }
 out:
        if (installed != NULL)


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