From f28477bf6cfcd7b522d2003a931ef2daa047d2c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Kozio=C5=82?= Date: Sun, 15 Nov 2026 13:58:02 +0100 Subject: [PATCH] Popovers never outlive their parent: unparented when it goes first MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A popover was unparented in an idle callback after it closed. When its parent went while it was open — the plugin browser's category row, with the sidebar rebuilt by a favourite, a plugin check or a rescan — the popover kept pointing at the freed row, and gtk_widget_unparent crashed (SIGSEGV in the idle callback, from plugin_browser's ask_name). canvas::attach_popover parents a transient popover for as long as it is shown: unparented once it closes, or right when its parent is destroyed, whichever comes first; the idle then finds nothing to do. Every popover the shell parents by hand uses it: the canvas menus and text prompts, the plugin browser's name prompt, MIDI learn, the toggles' submenus and the plugin window's parameter menu. Co-Authored-By: Claude Opus 5.5 --- crates/faderframe-ui/src/canvas.rs | 63 ++++++++++++++++--- crates/faderframe-ui/src/learn.rs | 6 +- crates/faderframe-ui/src/plugin_browser.rs | 6 +- crates/faderframe-ui/src/plugin_window/mod.rs | 3 +- crates/faderframe-ui/src/toggles.rs | 4 +- 5 files changed, 58 insertions(+), 24 deletions(-) diff --git a/crates/faderframe-ui/src/canvas.rs b/crates/faderframe-ui/src/canvas.rs index 5f98b7f2..4721df58 100644 --- a/crates/faderframe-ui/src/canvas.rs +++ b/crates/faderframe-ui/src/canvas.rs @@ -999,7 +999,7 @@ impl CanvasWidget { entry.set_text(&initial); entry.set_width_chars(16); popover.set_child(Some(&entry)); - popover.set_parent(self); + attach_popover(&popover, self); popover.set_pointing_to(Some(&gdk::Rectangle::new( at.x as i32, at.y as i32, @@ -1019,10 +1019,6 @@ impl CanvasWidget { } } )); - popover.connect_closed(|p| { - let p = p.clone(); - glib::idle_add_local_once(move || p.unparent()); - }); popover.popup(); entry.grab_focus(); entry.select_region(0, -1); @@ -1674,6 +1670,32 @@ impl CanvasWidget { /// them and moving between levels never resizes or moves it (a popover /// that grows by the edge of the screen may otherwise be closed by the /// compositor). A long menu gets a search field over its levels. +/// Parent a transient popover to `parent` for as long as it is shown: +/// unparented once it closes (after GTK has finished closing it) — or, +/// when `parent` goes first (a list row rebuilt while the popover is +/// open), right then, so the popover never points at a freed widget +/// (unparenting one that did crashed GTK). +pub(crate) fn attach_popover(pop: &impl IsA, parent: &impl IsA) { + let pop: gtk::Popover = pop.clone().upcast(); + pop.set_parent(parent); + let weak = pop.downgrade(); + parent.connect_destroy(move |w| { + if let Some(p) = weak.upgrade() + && p.parent().as_ref() == Some(w.upcast_ref::()) + { + p.unparent(); + } + }); + pop.connect_closed(|p| { + let p = p.clone(); + glib::idle_add_local_once(move || { + if p.parent().is_some() { + p.unparent(); + } + }); + }); +} + pub fn show_menu( parent: >k::Widget, at: Point, @@ -1706,12 +1728,10 @@ pub fn show_menu( } None => popover.set_child(Some(&scroller)), } - popover.set_parent(parent); + attach_popover(&popover, parent); popover.set_pointing_to(Some(&gdk::Rectangle::new(at.x as i32, at.y as i32, 1, 1))); - popover.connect_closed(|p| { + popover.connect_closed(|_| { OPEN_MENU.with(|m| m.borrow_mut().take()); - let p = p.clone(); - glib::idle_add_local_once(move || p.unparent()); }); OPEN_MENU.with(|m| { *m.borrow_mut() = Some(OpenMenu { @@ -2354,4 +2374,29 @@ mod tests { assert_eq!(super::named_files(&text), vec![a, b]); let _ = std::fs::remove_dir_all(&dir); } + + /// A popover whose parent goes while it is open (a sidebar row rebuilt) + /// is unparented then, and its closing later touches nothing freed: it + /// had crashed GTK in `gtk_widget_unparent` from the idle callback. + #[test] + #[ignore = "requires a GTK display"] + fn a_popover_outlives_its_parent_row_safely() { + use gtk::prelude::*; + gtk::init().expect("GTK display"); + let list = gtk::Box::new(gtk::Orientation::Vertical, 0); + let row = gtk::Label::new(Some("Category")); + list.append(&row); + let pop = gtk::Popover::new(); + super::attach_popover(&pop, &row); + assert_eq!(pop.parent().as_ref(), Some(row.upcast_ref::())); + // The row goes (only the list held it) while the popover is open. + list.remove(&row); + drop(row); + assert!(pop.parent().is_none(), "unparented with its row"); + // Closing it later, and the idle after, are harmless. + pop.emit_by_name::<()>("closed", &[]); + let ctx = gtk::glib::MainContext::default(); + while ctx.iteration(false) {} + assert!(pop.parent().is_none()); + } } diff --git a/crates/faderframe-ui/src/learn.rs b/crates/faderframe-ui/src/learn.rs index 47d6f8b8..b49797d9 100644 --- a/crates/faderframe-ui/src/learn.rs +++ b/crates/faderframe-ui/src/learn.rs @@ -40,7 +40,7 @@ pub fn on_right_click_with( let list = gtk::Box::new(gtk::Orientation::Vertical, 0); let popover = gtk::Popover::new(); popover.set_child(Some(&list)); - popover.set_parent(&anchor); + crate::canvas::attach_popover(&popover, &anchor); popover.set_has_arrow(true); for (k, (label, action)) in entries.into_iter().enumerate() { if k == gap && gap > 0 { @@ -63,10 +63,6 @@ pub fn on_right_click_with( }); list.append(&b); } - popover.connect_closed(|p| { - let p = p.clone(); - gtk::glib::idle_add_local_once(move || p.unparent()); - }); popover.popup(); }); widget.add_controller(click); diff --git a/crates/faderframe-ui/src/plugin_browser.rs b/crates/faderframe-ui/src/plugin_browser.rs index a7b5bee8..45b6a0af 100644 --- a/crates/faderframe-ui/src/plugin_browser.rs +++ b/crates/faderframe-ui/src/plugin_browser.rs @@ -561,7 +561,7 @@ fn ask_name( done: impl Fn(String) + 'static, ) -> gtk::Popover { let pop = gtk::Popover::new(); - pop.set_parent(parent); + crate::canvas::attach_popover(&pop, parent); let column = gtk::Box::new(gtk::Orientation::Vertical, 6); column.set_margin_top(4); column.set_margin_bottom(4); @@ -595,10 +595,6 @@ fn ask_name( let again = go.clone(); entry.connect_activate(move |_| again()); ok.connect_clicked(move |_| go()); - pop.connect_closed(|p| { - let p = p.clone(); - glib::idle_add_local_once(move || p.unparent()); - }); pop.popup(); entry.grab_focus(); pop diff --git a/crates/faderframe-ui/src/plugin_window/mod.rs b/crates/faderframe-ui/src/plugin_window/mod.rs index 18c9eed3..ea12a5d9 100644 --- a/crates/faderframe-ui/src/plugin_window/mod.rs +++ b/crates/faderframe-ui/src/plugin_window/mod.rs @@ -1878,9 +1878,8 @@ fn open_generic(app: &Rc, plugin: PluginInstanceId) { list.append(&b); } popover.set_child(Some(&list)); - popover.set_parent(&widget); + crate::canvas::attach_popover(&popover, &widget); popover.set_pointing_to(Some(>k::gdk::Rectangle::new(x as i32, y as i32, 1, 1))); - popover.connect_closed(|p| p.unparent()); popover.popup(); }); row.row.add_controller(menu); diff --git a/crates/faderframe-ui/src/toggles.rs b/crates/faderframe-ui/src/toggles.rs index 98802963..05aaf5bc 100644 --- a/crates/faderframe-ui/src/toggles.rs +++ b/crates/faderframe-ui/src/toggles.rs @@ -600,7 +600,7 @@ pub(crate) fn show_menu(app: &Rc, name: &str) { old.popdown(); } let popover = gtk::PopoverMenu::from_model(Some(&sub)); - popover.set_parent(&window); + crate::canvas::attach_popover(&popover, &window); popover.set_has_arrow(false); popover.set_position(gtk::PositionType::Bottom); popover.set_pointing_to(Some(>k::gdk::Rectangle::new(120, 4, 1, 1))); @@ -612,8 +612,6 @@ pub(crate) fn show_menu(app: &Rc, name: &str) { m.take(); } }); - let p = p.clone(); - glib::idle_add_local_once(move || p.unparent()); }); SHOWN.with(|m| *m.borrow_mut() = Some(popover)); }