window: Keep a reference to move_focus_widget
authorMatthias Clasen <mclasen@redhat.com>
Tue, 18 Oct 2022 18:21:51 +0000 (14:21 -0400)
committerMat <mail@mathias.is>
Fri, 21 Oct 2022 11:37:43 +0000 (14:37 +0300)
If we don't take a reference, it can happen that
the pointer is no longer valid by the time we
dereference it in after_paint.

gtk/gtkwindow.c

index 1de17f90487235c176542bf9f3793118f66febbf..6b4e2d77a764ef13203c379a67e05bbf37f8b739 100644 (file)
@@ -2006,7 +2006,10 @@ gtk_window_root_set_focus (GtkRoot   *root,
     {
       if (priv->move_focus &&
           focus && gtk_widget_is_visible (focus))
-        priv->move_focus = FALSE;
+        {
+          priv->move_focus = FALSE;
+          g_clear_object (&priv->move_focus_widget);
+        }
       return;
     }
 
@@ -2028,7 +2031,10 @@ gtk_window_root_set_focus (GtkRoot   *root,
 
   if (priv->move_focus &&
       focus && gtk_widget_is_visible (focus))
-    priv->move_focus = FALSE;
+    {
+      priv->move_focus = FALSE;
+      g_clear_object (&priv->move_focus_widget);
+    }
 
   g_object_notify (G_OBJECT (self), "focus-widget");
 }
@@ -2552,6 +2558,7 @@ gtk_window_dispose (GObject *object)
   g_list_free_full (priv->foci, (GDestroyNotify) gtk_pointer_focus_unref);
   priv->foci = NULL;
 
+  g_clear_object (&priv->move_focus_widget);
   gtk_window_set_focus (window, NULL);
   gtk_window_set_default_widget (window, NULL);
 
@@ -4695,6 +4702,9 @@ maybe_unset_focus_and_default (GtkWindow *window)
 
           parent = _gtk_widget_get_parent (parent);
         }
+
+      priv->move_focus = FALSE;
+      g_clear_object (&priv->move_focus_widget);
     }
 
   if (priv->unset_default)
@@ -5155,7 +5165,7 @@ _gtk_window_unset_focus_and_default (GtkWindow *window,
   child = priv->focus_widget;
   if (child && (child == widget || gtk_widget_is_ancestor (child, widget)))
     {
-      priv->move_focus_widget = widget;
+      priv->move_focus_widget = g_object_ref (widget);
       priv->move_focus = TRUE;
     }