dispose_template causes unparenting twice when using widget subclasses
Steps to reproduce
- Compile and run this snippet: https://gitlab.gnome.org/-/snippets/5743
- Close the window
- See the critical:
(test:487081): Gtk-CRITICAL **: 11:31:54.392: gtk_widget_unparent: assertion 'GTK_IS_WIDGET (widget)' failed
Current behavior
Partial backtrace of the critical:
...
#2 0x00007ffff7884327 in gtk_window_dispose (object=0x4ff960) at ../gtk/gtkwindow.c:2563
#3 0x00000000004013fd in example_template_dispose (object=0x4ff960) at ../main.c:36
#4 0x00007ffff7479974 in g_object_unref (_object=0x4ff960) at ../gobject/gobject.c:3891
#5 g_object_unref (_object=0x4ff960) at ../gobject/gobject.c:3802
#6 0x00007ffff7aa105d in gtk_widget_stack_free_elements (end=<optimized out>, start=<optimized out>) at ../gdk/gdkarrayimpl.c:95
#7 gtk_widget_stack_clear (self=0x7fffffffd290) at ../gdk/gdkarrayimpl.c:104
...
On the line:
2563 g_clear_pointer (&priv->child, gtk_widget_unparent);
So the child is unparented (and freed) already in gtk_widget_dispose_template()
by the time gtk_window_dispose()
runs, but GtkWindow
still holds a dangling pointer to the child.
Expected outcome
gtk_widget_dispose_template()
should not unparent child widgets that are already "owned" by a widget subclass. But it is unclear how to figure that out, without changing the implementation of every buildable_add_child
.
Version information
GTK 4.11.1, Fedora rawhide