From 5cbe681a4f24de7ba1b997784da28ee1e97bdc02 Mon Sep 17 00:00:00 2001
From: Sweet Tea Dorminy <sweettea-kernel@dorminy.me>
Date: Sat, 21 Nov 2026 18:19:18 -0800
Subject: [PATCH] gtk4: Drop the grid reference taken when detaching a tab

gui_dialog_detach() takes a reference on the dialog's grid so that
removing the notebook page does not destroy it, and never drops it.
Destroying the window then leaves the grid, its contents and the
gui_dialog alive and in the dialog list. For a detached Diplomacy
tab, gui_dialog_destroy_all() at disconnect or game end later
clears the owner's pointer inside the already freed dipl_main.

Drop the reference once the window holds the grid.

See RM #2499

Signed-off-by: Sweet Tea Dorminy <sweettea-kernel@dorminy.me>
---
 client/gui-gtk-4.0/gui_stuff.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/client/gui-gtk-4.0/gui_stuff.c b/client/gui-gtk-4.0/gui_stuff.c
index dd6ed41d1f..9cdb500695 100644
--- a/client/gui-gtk-4.0/gui_stuff.c
+++ b/client/gui-gtk-4.0/gui_stuff.c
@@ -449,6 +449,9 @@ static void gui_dialog_detach(struct gui_dialog *dlg)
   setup_dialog(window, toplevel);
 
   gtk_window_set_child(GTK_WINDOW(window), dlg->grid);
+  /* The window holds the grid now. Drop our reference so that destroying
+   * the window destroys the grid, and gui_dialog_destroy_handler() runs. */
+  g_object_unref(dlg->grid);
   dlg->v.window = window;
   g_signal_connect(window, "close-request",
                    G_CALLBACK(gui_dialog_delete_handler), dlg);
-- 
2.55.0

