[nautilus/wip/antoniof/meets-valgrind: 7/11] tag-manager: Don't leak singleton references
- From: António Fernandes <antoniof src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [nautilus/wip/antoniof/meets-valgrind: 7/11] tag-manager: Don't leak singleton references
- Date: Mon, 21 Dec 2020 18:35:05 +0000 (UTC)
commit f07f8cc5482c1a7a8d0a68125097bd66d8f0c6c2
Author: António Fernandes <antoniof gnome org>
Date: Mon Dec 21 17:21:28 2020 +0000
tag-manager: Don't leak singleton references
We are leaking references to the singleton object in multiple places.
It's not a problem during runtime, but this means it's not properly
finalized when the application quits, leaking the tracker connection,
which is detected by valgrind.
Add g_autoptr() and g_clear_object() where missing. Also, to prevent
future leaks, document nautilus_tag_manager_get() and uniformize its
use as either an autovariable initializer or a field initializer.
src/nautilus-file-undo-operations.c | 8 ++------
src/nautilus-file.c | 4 +---
src/nautilus-files-view.c | 1 +
src/nautilus-list-view.c | 1 +
src/nautilus-starred-directory.c | 8 ++------
src/nautilus-tag-manager.c | 13 +++++++++++--
6 files changed, 18 insertions(+), 17 deletions(-)
---
diff --git a/src/nautilus-file-undo-operations.c b/src/nautilus-file-undo-operations.c
index b263a33d3..a6a3b2025 100644
--- a/src/nautilus-file-undo-operations.c
+++ b/src/nautilus-file-undo-operations.c
@@ -1437,9 +1437,7 @@ starred_redo_func (NautilusFileUndoInfo *info,
NautilusFileOperationsDBusData *dbus_data)
{
NautilusFileUndoInfoStarred *self = NAUTILUS_FILE_UNDO_INFO_STARRED (info);
- NautilusTagManager *tag_manager;
-
- tag_manager = nautilus_tag_manager_get ();
+ g_autoptr (NautilusTagManager) tag_manager = nautilus_tag_manager_get ();
if (self->starred)
{
@@ -1465,9 +1463,7 @@ starred_undo_func (NautilusFileUndoInfo *info,
NautilusFileOperationsDBusData *dbus_data)
{
NautilusFileUndoInfoStarred *self = NAUTILUS_FILE_UNDO_INFO_STARRED (info);
- NautilusTagManager *tag_manager;
-
- tag_manager = nautilus_tag_manager_get ();
+ g_autoptr (NautilusTagManager) tag_manager = nautilus_tag_manager_get ();
if (self->starred)
{
diff --git a/src/nautilus-file.c b/src/nautilus-file.c
index 3ba887e8d..88b28c61b 100644
--- a/src/nautilus-file.c
+++ b/src/nautilus-file.c
@@ -3476,14 +3476,12 @@ static int
compare_by_starred (NautilusFile *file_1,
NautilusFile *file_2)
{
- NautilusTagManager *tag_manager;
+ g_autoptr (NautilusTagManager) tag_manager = nautilus_tag_manager_get ();
g_autofree gchar *uri_1 = NULL;
g_autofree gchar *uri_2 = NULL;
gboolean file_1_is_starred;
gboolean file_2_is_starred;
- tag_manager = nautilus_tag_manager_get ();
-
uri_1 = nautilus_file_get_uri (file_1);
uri_2 = nautilus_file_get_uri (file_2);
diff --git a/src/nautilus-files-view.c b/src/nautilus-files-view.c
index 4b8502557..1e5d0fd2d 100644
--- a/src/nautilus-files-view.c
+++ b/src/nautilus-files-view.c
@@ -3327,6 +3327,7 @@ nautilus_files_view_finalize (GObject *object)
g_cancellable_cancel (priv->starred_cancellable);
g_clear_object (&priv->starred_cancellable);
+ g_clear_object (&priv->tag_manager);
G_OBJECT_CLASS (nautilus_files_view_parent_class)->finalize (object);
}
diff --git a/src/nautilus-list-view.c b/src/nautilus-list-view.c
index 66e3373e2..a4fabb924 100644
--- a/src/nautilus-list-view.c
+++ b/src/nautilus-list-view.c
@@ -3743,6 +3743,7 @@ nautilus_list_view_finalize (GObject *object)
g_cancellable_cancel (list_view->details->starred_cancellable);
g_clear_object (&list_view->details->starred_cancellable);
+ g_clear_object (&list_view->details->tag_manager);
g_signal_handlers_disconnect_by_func (list_view->details->tag_manager,
on_starred_files_changed,
diff --git a/src/nautilus-starred-directory.c b/src/nautilus-starred-directory.c
index c1a3fe534..fd41418a4 100644
--- a/src/nautilus-starred-directory.c
+++ b/src/nautilus-starred-directory.c
@@ -568,16 +568,12 @@ nautilus_starred_directory_new ()
static void
nautilus_starred_directory_init (NautilusFavoriteDirectory *self)
{
- NautilusTagManager *tag_manager;
-
- tag_manager = nautilus_tag_manager_get ();
+ self->tag_manager = nautilus_tag_manager_get ();
- g_signal_connect (tag_manager,
+ g_signal_connect (self->tag_manager,
"starred-changed",
(GCallback) on_starred_files_changed,
self);
- self->tag_manager = tag_manager;
-
nautilus_starred_directory_set_files (self);
}
diff --git a/src/nautilus-tag-manager.c b/src/nautilus-tag-manager.c
index 24c2de616..b3e79cdeb 100644
--- a/src/nautilus-tag-manager.c
+++ b/src/nautilus-tag-manager.c
@@ -598,6 +598,16 @@ nautilus_tag_manager_class_init (NautilusTagManagerClass *klass)
G_TYPE_POINTER);
}
+/**
+ * nautilus_tag_manager_get:
+ *
+ * Gets a reference to the tag manager.
+ *
+ * If used to initialize a struct field, make sure to release on finalization.
+ * If used to initialize a local variable, make sure to use g_autoptr().
+ *
+ * Returns: (transfer full): the #NautilusTagManager singleton object.
+ */
NautilusTagManager *
nautilus_tag_manager_get (void)
{
@@ -736,7 +746,7 @@ update_moved_uris_callback (GObject *object,
else
{
g_autolist (NautilusFile) updated_files = NULL;
- g_autoptr (NautilusTagManager) tag_manager = NULL;
+ g_autoptr (NautilusTagManager) tag_manager = nautilus_tag_manager_get ();
for (guint i = 0; i < new_uris->len; i++)
{
@@ -745,7 +755,6 @@ update_moved_uris_callback (GObject *object,
updated_files = g_list_prepend (updated_files, nautilus_file_get_by_uri (new_uri));
}
- tag_manager = nautilus_tag_manager_get ();
g_signal_emit_by_name (tag_manager, "starred-changed", updated_files);
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]