[xdg-desktop-portal-gnome/mwleeds/use-object-ref-sink: 10/10] Tweak memory management a bit




commit 0f765fba2f62e30de38910451cf0316fff3cffc4
Author: Phaedrus Leeds <mwleeds protonmail com>
Date:   Wed Mar 16 12:03:13 2022 -0700

    Tweak memory management a bit
    
    Use g_object_ref_sink() for widgets since that is the right pattern for
    GInitiallyUnowned objects, remove the corresponding
    g_object_unref/g_clear_object in the free func, and make more use of
    g_clear_object/g_clear_pointer.
    
    For the screenshot portal this is a correctness fix since in that case
    the g_clear_pointer() in screenshot_dialog_handle_close() was preventing
    us from dropping the reference held on the dialog that we would
    otherwise drop in screenshot_dialog_handle_free(). Otherwise these
    changes should not introduce any functional difference.

 src/account.c     | 13 ++++++-------
 src/appchooser.c  |  9 ++++-----
 src/filechooser.c |  7 +++----
 src/print.c       |  9 ++++-----
 src/screenshot.c  |  5 ++---
 src/wallpaper.c   | 16 +++++++++-------
 6 files changed, 28 insertions(+), 31 deletions(-)
---
diff --git a/src/account.c b/src/account.c
index cf0ded9..49ab06a 100644
--- a/src/account.c
+++ b/src/account.c
@@ -46,11 +46,10 @@ account_dialog_handle_free (gpointer data)
   AccountDialogHandle *handle = data;
 
   g_clear_object (&handle->external_parent);
-  g_object_unref (handle->request);
-  g_object_unref (handle->dialog);
-  g_free (handle->user_name);
-  g_free (handle->real_name);
-  g_free (handle->icon_uri);
+  g_clear_object (&handle->request);
+  g_clear_pointer (&handle->user_name, g_free);
+  g_clear_pointer (&handle->real_name, g_free);
+  g_clear_pointer (&handle->icon_uri, g_free);
 
   g_free (handle);
 }
@@ -58,7 +57,7 @@ account_dialog_handle_free (gpointer data)
 static void
 account_dialog_handle_close (AccountDialogHandle *handle)
 {
-  gtk_window_destroy (GTK_WINDOW (handle->dialog));
+  g_clear_pointer (&handle->dialog, gtk_window_destroy);
   account_dialog_handle_free (handle);
 }
 
@@ -198,7 +197,7 @@ handle_get_user_information (XdpImplAccount        *object,
   handle->impl = object;
   handle->invocation = invocation;
   handle->request = g_object_ref (request);
-  handle->dialog = g_object_ref (dialog);
+  handle->dialog = g_object_ref_sink (dialog);
   handle->external_parent = external_parent;
   handle->user_name = g_strdup (user_name);
   handle->real_name = g_strdup (real_name);
diff --git a/src/appchooser.c b/src/appchooser.c
index 08f6b87..8a14431 100644
--- a/src/appchooser.c
+++ b/src/appchooser.c
@@ -62,9 +62,8 @@ app_dialog_handle_free (gpointer data)
 
   g_hash_table_remove (handles, handle->request->id);
   g_clear_object (&handle->external_parent);
-  g_object_unref (handle->request);
-  g_object_unref (handle->dialog);
-  g_free (handle->chosen);
+  g_clear_object (&handle->request);
+  g_clear_pointer (&handle->chosen, g_free);
 
   g_free (handle);
 }
@@ -72,7 +71,7 @@ app_dialog_handle_free (gpointer data)
 static void
 app_dialog_handle_close (AppDialogHandle *handle)
 {
-  gtk_window_destroy (GTK_WINDOW (handle->dialog));
+  g_clear_pointer (&handle->dialog, gtk_window_destroy);
   app_dialog_handle_free (handle);
 }
 
@@ -203,7 +202,7 @@ handle_choose_application (XdpImplAppChooser *object,
   handle->impl = object;
   handle->invocation = invocation;
   handle->request = g_object_ref (request);
-  handle->dialog = g_object_ref (dialog);
+  handle->dialog = g_object_ref_sink (dialog);
   handle->external_parent = external_parent;
 
   g_hash_table_insert (handles, handle->request->id, handle);
diff --git a/src/filechooser.c b/src/filechooser.c
index 84add23..fa9683d 100644
--- a/src/filechooser.c
+++ b/src/filechooser.c
@@ -72,8 +72,7 @@ file_dialog_handle_free (gpointer data)
   FileDialogHandle *handle = data;
 
   g_clear_object (&handle->external_parent);
-  g_object_unref (handle->dialog);
-  g_object_unref (handle->request);
+  g_clear_object (&handle->request);
   g_slist_free_full (handle->files, g_free);
   g_slist_free_full (handle->uris, g_free);
   g_hash_table_unref (handle->choices);
@@ -84,7 +83,7 @@ file_dialog_handle_free (gpointer data)
 static void
 file_dialog_handle_close (FileDialogHandle *handle)
 {
-  gtk_window_destroy (GTK_WINDOW (handle->dialog));
+  g_clear_pointer (&handle->dialog, gtk_window_destroy);
   file_dialog_handle_free (handle);
 }
 
@@ -482,7 +481,7 @@ handle_open (XdpImplFileChooser    *object,
   handle->impl = object;
   handle->invocation = invocation;
   handle->request = g_object_ref (request);
-  handle->dialog = g_object_ref (dialog);
+  handle->dialog = g_object_ref_sink (dialog);
   handle->action = action;
   handle->multiple = multiple;
   handle->choices = g_hash_table_new (g_str_hash, g_str_equal);
diff --git a/src/print.c b/src/print.c
index 327e228..2387f5a 100644
--- a/src/print.c
+++ b/src/print.c
@@ -159,8 +159,7 @@ print_dialog_handle_free (gpointer data)
   PrintDialogHandle *handle = data;
 
   g_clear_object (&handle->external_parent);
-  g_object_unref (handle->request);
-  g_object_unref (handle->dialog);
+  g_clear_object (&handle->request);
   close (handle->fd);
 
   g_free (handle);
@@ -169,7 +168,7 @@ print_dialog_handle_free (gpointer data)
 static void
 print_dialog_handle_close (PrintDialogHandle *handle)
 {
-  gtk_window_destroy (GTK_WINDOW (handle->dialog));
+  g_clear_pointer (&handle->dialog, gtk_window_destroy);
   print_dialog_handle_free (handle);
 }
 
@@ -518,7 +517,7 @@ handle_print (XdpImplPrint          *object,
   handle->impl = object;
   handle->invocation = invocation;
   handle->request = g_object_ref (request);
-  handle->dialog = g_object_ref (dialog);
+  handle->dialog = g_object_ref_sink (dialog);
   handle->external_parent = external_parent;
   handle->fd = fd;
 
@@ -678,7 +677,7 @@ handle_prepare_print (XdpImplPrint          *object,
   handle->impl = object;
   handle->invocation = invocation;
   handle->request = g_object_ref (request);
-  handle->dialog = g_object_ref (dialog);
+  handle->dialog = g_object_ref_sink (dialog);
   handle->external_parent = external_parent;
 
   g_signal_connect (request, "handle-close", G_CALLBACK (handle_close), handle);
diff --git a/src/screenshot.c b/src/screenshot.c
index 5d49cdc..998d138 100644
--- a/src/screenshot.c
+++ b/src/screenshot.c
@@ -46,8 +46,7 @@ screenshot_dialog_handle_free (gpointer data)
 
   g_clear_object (&handle->external_parent);
   g_clear_object (&handle->request);
-  g_clear_object (&handle->dialog);
-  g_free (handle->uri);
+  g_clear_pointer (&handle->uri, g_free);
 
   g_free (handle);
 }
@@ -199,7 +198,7 @@ handle_screenshot (XdpImplScreenshot *object,
   handle->impl = object;
   handle->invocation = invocation;
   handle->request = g_object_ref (request);
-  handle->dialog = g_object_ref (dialog);
+  handle->dialog = g_object_ref_sink (dialog);
   handle->external_parent = external_parent;
   handle->retval = "url";
 
diff --git a/src/wallpaper.c b/src/wallpaper.c
index 217a61d..de93ff2 100644
--- a/src/wallpaper.c
+++ b/src/wallpaper.c
@@ -47,14 +47,16 @@ wallpaper_dialog_handle_free (gpointer data)
   g_clear_object (&handle->request);
   g_clear_pointer (&handle->picture_uri, g_free);
 
-  if (handle->dialog != NULL)
-      gtk_window_destroy (GTK_WINDOW (handle->dialog));
-  g_clear_object (&handle->dialog);
-
-
   g_free (handle);
 }
 
+static void
+wallpaper_dialog_handle_close (WallpaperDialogHandle *handle)
+{
+  g_clear_pointer (&handle->dialog, gtk_window_destroy);
+  wallpaper_dialog_handle_free (handle);
+}
+
 static void
 send_response (WallpaperDialogHandle *handle)
 {
@@ -65,7 +67,7 @@ send_response (WallpaperDialogHandle *handle)
                                                  handle->invocation,
                                                  handle->response);
 
-  wallpaper_dialog_handle_free (handle);
+  wallpaper_dialog_handle_close (handle);
 }
 
 static gboolean
@@ -222,7 +224,7 @@ handle_set_wallpaper_uri (XdpImplWallpaper *object,
 
   dialog = (GtkWidget *)wallpaper_dialog_new (arg_uri, arg_app_id);
   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (fake_parent));
-  handle->dialog = g_object_ref (dialog);
+  handle->dialog = g_object_ref_sink (dialog);
 
   g_signal_connect (dialog, "response",
                     G_CALLBACK (handle_wallpaper_dialog_response), handle);


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