a11y: Use weak references for GtkATContext cached accessibles
authorEmmanuele Bassi <ebassi@gnome.org>
Fri, 3 Mar 2023 21:10:39 +0000 (21:10 +0000)
committerEmmanuele Bassi <ebassi@gnome.org>
Fri, 3 Mar 2023 22:07:09 +0000 (22:07 +0000)
The accessible objects already own the GtkATContext, let's avoid a
reference cycle.

gtk/gtkatcontext.c

index e4e66e82aa86067d8ac1f89a3562bd45f93c048f..2ae924cc92dea11eec48255b13e49beb709b190a 100644 (file)
@@ -85,8 +85,19 @@ gtk_at_context_dispose (GObject *gobject)
 
   gtk_at_context_unrealize (self);
 
-  g_clear_object (&self->accessible_parent);
-  g_clear_object (&self->next_accessible_sibling);
+  if (self->accessible_parent != NULL)
+    {
+      g_object_remove_weak_pointer (G_OBJECT (self->accessible_parent),
+                                    (gpointer *) &self->accessible_parent);
+      self->accessible_parent = NULL;
+    }
+
+  if (self->next_accessible_sibling != NULL)
+    {
+      g_object_remove_weak_pointer (G_OBJECT (self->next_accessible_sibling),
+                                    (gpointer *) &self->next_accessible_sibling);
+      self->next_accessible_sibling = NULL;
+    }
 
   G_OBJECT_CLASS (gtk_at_context_parent_class)->dispose (gobject);
 }
@@ -479,7 +490,17 @@ gtk_at_context_set_accessible_parent (GtkATContext *self,
 {
   g_return_if_fail (GTK_IS_AT_CONTEXT (self));
 
-  g_set_object (&self->accessible_parent, parent);
+  if (self->accessible_parent != parent)
+    {
+      if (self->accessible_parent != NULL)
+        g_object_remove_weak_pointer (G_OBJECT (self->accessible_parent),
+                                      (gpointer *) &self->accessible_parent);
+
+      self->accessible_parent = parent;
+      if (self->accessible_parent != NULL)
+        g_object_add_weak_pointer (G_OBJECT (self->accessible_parent),
+                                   (gpointer *) &self->accessible_parent);
+    }
 }
 
 /*< private >
@@ -511,7 +532,18 @@ gtk_at_context_set_next_accessible_sibling (GtkATContext *self,
 {
   g_return_if_fail (GTK_IS_AT_CONTEXT (self));
 
-  g_set_object (&self->next_accessible_sibling, sibling);
+  if (self->next_accessible_sibling != sibling)
+    {
+      if (self->next_accessible_sibling != NULL)
+        g_object_remove_weak_pointer (G_OBJECT (self->next_accessible_sibling),
+                                      (gpointer *) &self->next_accessible_sibling);
+
+      self->next_accessible_sibling = sibling;
+
+      if (self->next_accessible_sibling != NULL)
+        g_object_add_weak_pointer (G_OBJECT (self->next_accessible_sibling),
+                                   (gpointer *) &self->next_accessible_sibling);
+    }
 }
 
 /*< private >