[glib] Make g_set_object more symmetric



commit 087d75e3c336547fff37a4bc5d33b1ad041485bf
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Sep 7 19:43:19 2015 -0400

    Make g_set_object more symmetric
    
    As argued in bug 748633, and order of ref, assign, unref is
    preferable.

 gobject/gobject.h |   12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)
---
diff --git a/gobject/gobject.h b/gobject/gobject.h
index a889d3c..32ab997 100644
--- a/gobject/gobject.h
+++ b/gobject/gobject.h
@@ -690,21 +690,25 @@ static inline gboolean
 (g_set_object) (GObject **object_ptr,
                 GObject  *new_object)
 {
+  GObject *old_object = *object_ptr;
+
   /* rely on g_object_[un]ref() to check the pointers are actually GObjects;
    * elide a (object_ptr != NULL) check because most of the time we will be
    * operating on struct members with a constant offset, so a NULL check would
-   * not catch bugs */
+   * not catch bugs
+   */
 
-  if (*object_ptr == new_object)
+  if (old_object == new_object)
     return FALSE;
 
   if (new_object != NULL)
     g_object_ref (new_object);
-  if (*object_ptr != NULL)
-    g_object_unref (*object_ptr);
 
   *object_ptr = new_object;
 
+  if (old_object != NULL)
+    g_object_unref (old_object);
+
   return TRUE;
 }
 


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