From 7d9611fdf72afa0247fedccdb0a7450148c5664a Mon Sep 17 00:00:00 2001 From: Blake Gardner Date: Sat, 28 Nov 2026 23:58:17 -0600 Subject: [PATCH 1/2] fix: tear down the panel and dock when they are destroyed Moving the shell to another display rebuilds the panel and dock. In GTK4 destroy() only hides a window and drops it from the toplevel list; dispose() waits for the last reference, and the signal closures that capture the panel and dock keep them alive. Their cleanup lived in dispose(), so it never ran: the replaced surfaces kept answering fullscreen and focus signals and presented themselves again when a fullscreen window closed, leaving one extra panel and dock on screen per earlier move. Run the teardown from destroy(), disconnect the panel's AppSystem, HomeCover and wallpaper handlers there, and refuse to show a panel or dock that is no longer in the toplevel list. Assisted-by: Claude:claude-fable-5-2 AI-Scope: diagnosis from shell logs and a temporary show/map probe; destroy-time teardown and show guard in panel, dock and layer_window --- src/components/dock/dock.vala | 39 +++++++++++++++++++++-- src/components/panel/panel.vala | 55 +++++++++++++++++++++++++++------ src/core/layer_window.vala | 17 ++++++++++ 3 files changed, 100 insertions(+), 11 deletions(-) diff --git a/src/components/dock/dock.vala b/src/components/dock/dock.vala index e13c4762..598a8c1e 100644 --- a/src/components/dock/dock.vala +++ b/src/components/dock/dock.vala @@ -39,6 +39,10 @@ namespace Singularity { private bool _refreshed_once = false; private bool _intro_pending = false; private bool _hidden_for_fullscreen = false; + // Set by teardown(), which destroy() runs. A destroyed dock stays alive + // while any closure still references it, and a GTK4 window can be shown + // again after destroy(), so every path that maps the dock checks this. + private bool _disposed = false; private ulong _sig_clock = 0; private bool is_primary = true; private Gdk.Monitor? gdk_monitor = null; @@ -901,7 +905,22 @@ namespace Singularity { return int.max(0, body) + _rest_margin; } + // Every present() in this class goes through here, so a late signal or + // timer cannot bring a destroyed dock back on screen. + public new void present() { + if (_disposed) return; + base.present(); + } + + // Whoever asks (present(), set_visible(), a property binding), a + // destroyed dock never maps again. + public override void show() { + if (_disposed || window_was_destroyed(this)) return; + base.show(); + } + private void update_autohide_state() { + if (_disposed) return; if (hidden_by_scrolling_tiling()) { _set_reveal_barrier_active(false); return; @@ -1140,6 +1159,7 @@ namespace Singularity { // slid-off dock receives no pointer events on this compositor, so this // always-on-screen strip is what catches the cursor to reveal the dock. private void _set_reveal_barrier_active(bool active) { + if (_disposed) return; if (!active) { if (_reveal_barrier != null) _reveal_barrier.set_visible(false); return; @@ -1190,6 +1210,7 @@ namespace Singularity { } private void update_visibility_mode() { + if (_disposed) return; if (hidden_by_scrolling_tiling()) { close_layer_window(this); set_exclusive_zone(this, 0); @@ -1255,7 +1276,7 @@ namespace Singularity { } private void update_fullscreen_mode() { - if (hidden_by_scrolling_tiling()) return; + if (_disposed || hidden_by_scrolling_tiling()) return; bool fs = is_any_window_fullscreen_on_my_monitor(); if (_settings.get_boolean("bar-layout-edit-mode")) { _hidden_for_fullscreen = fs; @@ -3131,7 +3152,17 @@ namespace Singularity { main_container.add_controller(rt); } - protected override void dispose() { + // destroy() does not run dispose() in GTK4 (see window_was_destroyed), + // so the teardown has to happen here, not only when the last reference + // goes: otherwise a replaced dock keeps answering signals. + public new void destroy() { + teardown(); + base.destroy(); + } + + private void teardown() { + if (_disposed) return; + _disposed = true; var as = app_system; if (_sig_config_changed != 0) { GLib.SignalHandler.disconnect(as, _sig_config_changed); _sig_config_changed = 0; } if (_sig_apps_changed != 0) { GLib.SignalHandler.disconnect(as, _sig_apps_changed); _sig_apps_changed = 0; } @@ -3177,6 +3208,10 @@ namespace Singularity { _reveal_barrier.destroy(); _reveal_barrier = null; } + } + + protected override void dispose() { + teardown(); base.dispose(); } diff --git a/src/components/panel/panel.vala b/src/components/panel/panel.vala index 52776a0b..3ce3a3ac 100644 --- a/src/components/panel/panel.vala +++ b/src/components/panel/panel.vala @@ -466,6 +466,13 @@ namespace Singularity { private Gdk.Monitor? gdk_monitor = null; private string? last_monitor_app_id = null; private ulong _sig_clock = 0; + // Set by teardown(), which destroy() runs. A destroyed panel stays alive + // while any closure still references it, and a GTK4 window can be shown + // again after destroy(), so every path that maps the panel checks this. + private bool _disposed = false; + private ulong[] _sig_app_sys = {}; + private ulong[] _sig_home_cover = {}; + private ulong _sig_wallpaper = 0; private bool _hidden_for_fullscreen = false; private ulong _sig_app_focused = 0; private ulong _sig_menu_model_changed = 0; @@ -939,29 +946,29 @@ namespace Singularity { _last_strip_lum = -1.0; update_topbar_fg_class(); }); - WallpaperManager.get_default().wallpaper_changed.connect(() => { + _sig_wallpaper = WallpaperManager.get_default().wallpaper_changed.connect(() => { _last_strip_lum = -1.0; update_topbar_fg_class(); }); // Auto-flatten when any window is maximized var app_sys = AppSystem.get_default(); - HomeCover.get_default().changed.connect((m) => { + _sig_home_cover += HomeCover.get_default().changed.connect((m) => { if (m == gdk_monitor) update_flat_mode(); }); - HomeCover.get_default().amount_changed.connect((m, amount) => { + _sig_home_cover += HomeCover.get_default().amount_changed.connect((m, amount) => { if (m == gdk_monitor) _hub_shade.shade = amount; }); - app_sys.any_maximized_changed.connect(() => { + _sig_app_sys += app_sys.any_maximized_changed.connect(() => { update_flat_mode(); }); - app_sys.app_closed.connect((handle) => { + _sig_app_sys += app_sys.app_closed.connect((handle) => { update_flat_mode(); update_fullscreen_mode(); }); - app_sys.any_fullscreen_changed.connect(() => { + _sig_app_sys += app_sys.any_fullscreen_changed.connect(() => { update_fullscreen_mode(); }); - app_sys.window_focused.connect(() => { + _sig_app_sys += app_sys.window_focused.connect(() => { update_fullscreen_mode(); }); update_visibility(); @@ -999,7 +1006,7 @@ namespace Singularity { } private void update_fullscreen_mode() { - if (is_greeter_mode) return; + if (_disposed || is_greeter_mode) return; bool fs = is_any_window_fullscreen_on_my_monitor(); if (_settings.get_boolean("bar-layout-edit-mode")) { _hidden_for_fullscreen = fs; @@ -1104,7 +1111,15 @@ namespace Singularity { update_topbar_fg_class(); } + // Whoever asks (present(), set_visible(), a property binding), a + // destroyed panel never maps again. + public override void show() { + if (_disposed || window_was_destroyed(this)) return; + base.show(); + } + private void update_visibility() { + if (_disposed) return; bool fusion = _settings.get_boolean("panel-fusion"); bool editing = !is_greeter_mode && _settings.get_boolean("bar-layout-edit-mode"); if (_hidden_for_fullscreen && !editing) { @@ -1399,7 +1414,25 @@ namespace Singularity { update_overview_surface_mode(); } - protected override void dispose() { + // destroy() does not run dispose() in GTK4 (see window_was_destroyed), + // so the teardown has to happen here, not only when the last reference + // goes: otherwise a replaced panel keeps answering signals. + public new void destroy() { + teardown(); + base.destroy(); + } + + private void teardown() { + if (_disposed) return; + _disposed = true; + foreach (ulong id in _sig_app_sys) GLib.SignalHandler.disconnect(AppSystem.get_default(), id); + _sig_app_sys = {}; + foreach (ulong id in _sig_home_cover) GLib.SignalHandler.disconnect(HomeCover.get_default(), id); + _sig_home_cover = {}; + if (_sig_wallpaper != 0) { + GLib.SignalHandler.disconnect(WallpaperManager.get_default(), _sig_wallpaper); + _sig_wallpaper = 0; + } var tiling = TilingManager.get_default(); if (tiling != null) cancel_tiling_hover(tiling); if (_tiling_organizer != null) { @@ -1421,6 +1454,10 @@ namespace Singularity { GLib.SignalHandler.disconnect(SharedClock.get_default(), _sig_clock); _sig_clock = 0; } + } + + protected override void dispose() { + teardown(); base.dispose(); } diff --git a/src/core/layer_window.vala b/src/core/layer_window.vala index cf2ae735..fee11ba5 100644 --- a/src/core/layer_window.vala +++ b/src/core/layer_window.vala @@ -30,6 +30,23 @@ namespace Singularity { * gtk_widget_unrealize. Verified by inspecting the C that valac emits for * each spelling. */ + /** + * Whether destroy() has already been called on a window. + * + * GTK4's gtk_window_destroy() only hides the window and drops it from the + * toplevel list. dispose() does not run until the last reference goes, and + * shell surfaces are kept alive by the signal closures that capture them, + * so a destroyed surface keeps receiving signals and can be shown again. + * Membership of the toplevel list is the one reliable marker. + */ + public bool window_was_destroyed (Gtk.Window window) { + var toplevels = Gtk.Window.get_toplevels (); + for (uint i = 0; i < toplevels.get_n_items (); i++) { + if (toplevels.get_item (i) == window) return false; + } + return true; + } + public void close_layer_window (Gtk.Window window) { ((Gtk.Widget) window).hide (); ((Gtk.Widget) window).unrealize (); From c63af3427c5ea8e01b452a351b25f3273d086a40 Mon Sep 17 00:00:00 2001 From: Blake Gardner Date: Sun, 29 Nov 2026 00:35:14 -0600 Subject: [PATCH 2/2] fix: release the panel and dock that a rebuild replaces A panel or dock that was destroyed for a rebuild was never freed. Lambdas connected on shared services held it, its own widgets and event controllers held it through their handlers, and the dock's motion hints held it through their alias callback. Each move of the shell to another display left one more of each in memory, still reacting to settings, network, audio and power changes. The dock also created a D-Bus service per instance. The first one kept the dev.sinty.Dock object path, so every later dock failed to register and apps kept talking to the service of a dock that was gone. Collect the handlers connected on outside objects in a SignalBag and disconnect them in the teardown, drop the motion hints there, and dispose the window tree once destroy() has returned. Make the dock D-Bus service one shared instance, so the registration happens once and the suffixes, icons and menus apps set survive a rebuild. Checked in a headless session with two outputs: ten moves of the shell between them release ten docks and nine panels with no GTK criticals. The newest replaced panel is still held until the following rebuild. Assisted-by: Claude:claude-fable-5-2 AI-Scope: leak analysis, SignalBag and window tree release helpers, handler tracking in panel and dock, shared dock D-Bus service; tested in a headless labwc session --- src/components/dock/dock.vala | 84 +++++++++++----- src/components/dock/dock_dbus_service.vala | 19 +++- src/components/panel/panel.vala | 107 ++++++++++++--------- src/core/layer_window.vala | 82 ++++++++++++++++ 4 files changed, 216 insertions(+), 76 deletions(-) diff --git a/src/components/dock/dock.vala b/src/components/dock/dock.vala index 598a8c1e..91bbc7e7 100644 --- a/src/components/dock/dock.vala +++ b/src/components/dock/dock.vala @@ -43,6 +43,7 @@ namespace Singularity { // while any closure still references it, and a GTK4 window can be shown // again after destroy(), so every path that maps the dock checks this. private bool _disposed = false; + private Singularity.SignalBag _signals = new Singularity.SignalBag(); private ulong _sig_clock = 0; private bool is_primary = true; private Gdk.Monitor? gdk_monitor = null; @@ -137,12 +138,12 @@ namespace Singularity { main_container.overflow = Overflow.VISIBLE; intro_motion = new Singularity.Animation.MotionBin(main_container); hide_motion = new Singularity.Animation.MotionBin(intro_motion); - HomeCover.get_default().shift_changed.connect((m, shift) => { + _signals.add(HomeCover.get_default(), HomeCover.get_default().shift_changed.connect((m, shift) => { var mon = get_target_monitor() ?? _shell_mon; if (m != mon || is_dock_vertical()) return; hide_motion.translate_x = shift; update_input_region(); - }); + })); dock_overlay.set_child(hide_motion); layout_surface = new Box(Orientation.HORIZONTAL, 0); @@ -188,18 +189,20 @@ namespace Singularity { dock_box.halign = Align.CENTER; magnifier = new DockMagnifier(dock_box, _settings); if (magnifier.enabled) add_css_class("magnify-active"); - magnifier.changed.connect(() => { + _signals.add(magnifier, magnifier.changed.connect(() => { if (magnifier.enabled) add_css_class("magnify-active"); else remove_css_class("magnify-active"); update_gap(); update_layout_viewports(); update_dock_reservation(); - }); + })); center_wrapper.append(dock_box); motion_hints = new DockMotionHints(this, dock_box, (id) => { return app_system.window_aliases(id); }); - dock_visibility_changed.connect((hidden) => motion_hints.set_hidden(hidden)); + dock_visibility_changed.connect((hidden) => { + if (motion_hints != null) motion_hints.set_hidden(hidden); + }); layout_items["applications"] = dock_box; // Resources (files / folders / links dropped on the @@ -238,10 +241,10 @@ namespace Singularity { { "overview", "applications", "system", "clock" }, { _("Overview"), _("Applications"), _("System Status"), _("Clock") } ); - layout_editor.move_requested.connect((item_id, section, index) => { + _signals.add(layout_editor, layout_editor.move_requested.connect((item_id, section, index) => { if (bar_layout != null && bar_layout.move(item_id, section, index)) save_bar_layout(); - }); - layout_editor.edit_mode_changed.connect(() => { + })); + _signals.add(layout_editor, layout_editor.edit_mode_changed.connect(() => { update_layout_viewports(); update_visibility_mode(); update_autohide_state(); @@ -249,18 +252,18 @@ namespace Singularity { set_layer(this, GtkLayerShell.Layer.BACKGROUND); } update_input_region(); - }); + })); update_layout_viewports(); add_css_class("dock-window"); _badges_shown = FocusManager.get_default().show_badges; - FocusManager.get_default().changed.connect(() => { + _signals.add(FocusManager.get_default(), FocusManager.get_default().changed.connect(() => { bool shown = FocusManager.get_default().show_badges; if (shown == _badges_shown) return; _badges_shown = shown; force_rebuild(); - }); + })); _sig_background_effect = _settings.changed["background-effect"].connect( update_background_effect); @@ -415,14 +418,14 @@ namespace Singularity { schedule_refresh(); var plugin_ctx = Singularity.PluginManager.get_default().get_context(); - plugin_ctx.dock_context_menu_provider_added.connect((provider) => { + _signals.add(plugin_ctx, plugin_ctx.dock_context_menu_provider_added.connect((provider) => { _menu_providers.add(provider); - }); - plugin_ctx.dock_context_menu_provider_removed.connect((provider) => { + })); + _signals.add(plugin_ctx, plugin_ctx.dock_context_menu_provider_removed.connect((provider) => { _menu_providers.remove(provider); - }); - plugin_ctx.dock_item_extension_added.connect(register_item_extension); - plugin_ctx.dock_item_extension_removed.connect(unregister_item_extension); + })); + _signals.add(plugin_ctx, plugin_ctx.dock_item_extension_added.connect(register_item_extension)); + _signals.add(plugin_ctx, plugin_ctx.dock_item_extension_removed.connect(unregister_item_extension)); // Load the kept-expanded set foreach (string s in _settings.get_strv("dock-pinned-suffixes")) { @@ -431,10 +434,9 @@ namespace Singularity { // Primary dock owns the DBus service so multi-monitor doesn't conflict if (is_primary) { - _dbus_service = new Singularity.DockDBusService(); - plugin_ctx.add_dock_item_extension(_dbus_service); - plugin_ctx.add_dock_context_menu_provider(_dbus_service); - _dbus_service.own_bus(); + _dbus_service = Singularity.DockDBusService.get_default(); + register_item_extension(_dbus_service); + _menu_providers.add(_dbus_service); } update_clock(); _sig_clock = SharedClock.get_default().minute_changed.connect(() => update_clock()); @@ -592,6 +594,7 @@ namespace Singularity { } private void update_layout_viewports() { + if (_disposed) return; bool editing = _settings.get_boolean("bar-layout-edit-mode"); bool vertical = is_dock_vertical(); int span = 0; @@ -621,6 +624,7 @@ namespace Singularity { } private void align_layout_viewports() { + if (_disposed) return; bool editing = _settings.get_boolean("bar-layout-edit-mode"); bool vertical = is_dock_vertical(); Gtk.Adjustment start_adjustment = vertical @@ -840,9 +844,9 @@ namespace Singularity { bat_icon.pixel_size = sys_icon_size; sys_box.append(bat_icon); - network.state_changed.connect(() => { net_icon.icon_name = network.wifi_icon; }); - audio.state_changed.connect(() => { audio_icon.icon_name = audio.icon_name; }); - power.state_changed.connect(() => { bat_icon.icon_name = power.icon_name; }); + _signals.add(network, network.state_changed.connect(() => { net_icon.icon_name = network.wifi_icon; })); + _signals.add(audio, audio.state_changed.connect(() => { audio_icon.icon_name = audio.icon_name; })); + _signals.add(power, power.state_changed.connect(() => { bat_icon.icon_name = power.icon_name; })); layout_items["system"] = sys_box; @@ -1393,6 +1397,7 @@ namespace Singularity { private bool _badges_shown = true; private void schedule_refresh() { + if (_disposed) return; if (_refresh_timeout_id != 0) GLib.Source.remove(_refresh_timeout_id); _refresh_timeout_id = GLib.Timeout.add(150, () => { _refresh_timeout_id = 0; @@ -1402,6 +1407,7 @@ namespace Singularity { } private void force_rebuild() { + if (_disposed) return; _item_cache.clear(); Widget? child = dock_box.get_first_child(); while (child != null) { @@ -2122,10 +2128,10 @@ namespace Singularity { btn.set_child(ws_icon); var sc_ws = SystemMonitor.get_default().shortcuts; btn.tooltip_text = sc_ws.tooltip_for(_("Workspaces"), "toggle_workspace_overview"); - sc_ws.shortcut_changed.connect((action, accel) => { + _signals.add(sc_ws, sc_ws.shortcut_changed.connect((action, accel) => { if (action == "toggle_workspace_overview") btn.tooltip_text = sc_ws.tooltip_for(_("Workspaces"), "toggle_workspace_overview"); - }); + })); btn.clicked.connect(() => { workspace_clicked(); }); pill.append(btn); animate_shell_button(wrapper, pill, btn); @@ -3158,11 +3164,37 @@ namespace Singularity { public new void destroy() { teardown(); base.destroy(); + // Not inline: the caller may still be inside one of our handlers. + GLib.Idle.add(() => { + release_window_tree(this); + return GLib.Source.REMOVE; + }); + } + + ~Dock() { + debug("Dock released"); } private void teardown() { if (_disposed) return; _disposed = true; + _signals.clear(); + // Its alias callback references this dock. + motion_hints = null; + foreach (var entry in _item_extension_handlers.entries) + GLib.SignalHandler.disconnect(entry.key, entry.value); + _item_extension_handlers.clear(); + _item_extensions.clear(); + _menu_providers.clear(); + _dbus_service = null; + if (_preview_dismiss_id != 0) { + GLib.Source.remove(_preview_dismiss_id); + _preview_dismiss_id = 0; + } + if (_preview_show_id != 0) { + GLib.Source.remove(_preview_show_id); + _preview_show_id = 0; + } var as = app_system; if (_sig_config_changed != 0) { GLib.SignalHandler.disconnect(as, _sig_config_changed); _sig_config_changed = 0; } if (_sig_apps_changed != 0) { GLib.SignalHandler.disconnect(as, _sig_apps_changed); _sig_apps_changed = 0; } diff --git a/src/components/dock/dock_dbus_service.vala b/src/components/dock/dock_dbus_service.vala index 6a22ad09..6f194375 100644 --- a/src/components/dock/dock_dbus_service.vala +++ b/src/components/dock/dock_dbus_service.vala @@ -29,7 +29,22 @@ namespace Singularity { private uint _own_id = 0; private uint _reg_id = 0; - public DockDBusService() {} + private static DockDBusService? _instance = null; + + // One service for the session, shared by every dock that is built: + // the bus name and object path can only be claimed once, and the + // suffixes, icons and menus apps registered have to survive the dock + // being rebuilt for another display. + [DBus (visible = false)] + public static DockDBusService get_default() { + if (_instance == null) { + _instance = new DockDBusService(); + _instance.own_bus(); + } + return _instance; + } + + private DockDBusService() {} private void track_owner(string nid, string sender) { // Drop any old watcher for this app_id. @@ -81,7 +96,7 @@ namespace Singularity { } } - public void own_bus() { + private void own_bus() { _own_id = Bus.own_name( BusType.SESSION, "dev.sinty.Dock", diff --git a/src/components/panel/panel.vala b/src/components/panel/panel.vala index 3ce3a3ac..3833e137 100644 --- a/src/components/panel/panel.vala +++ b/src/components/panel/panel.vala @@ -431,7 +431,7 @@ namespace Singularity { var gs = a11y_schema(t.schema); if (gs == null) continue; a11y_settings.insert(t.schema, gs); - gs.changed.connect(() => a11y_sync()); + _signals.add(gs, gs.changed.connect(() => a11y_sync())); } a11y_button = new Button.from_icon_name("preferences-desktop-accessibility-symbolic"); a11y_button.has_frame = false; @@ -440,7 +440,7 @@ namespace Singularity { a11y_button.add_css_class("accessibility-button"); a11y_button.tooltip_text = _("Accessibility"); a11y_button.clicked.connect(() => a11y_menu()); - _settings.changed["accessibility-menu"].connect(() => a11y_sync()); + _signals.add(_settings, _settings.changed["accessibility-menu"].connect(() => a11y_sync())); a11y_sync(); return a11y_button; } @@ -470,6 +470,7 @@ namespace Singularity { // while any closure still references it, and a GTK4 window can be shown // again after destroy(), so every path that maps the panel checks this. private bool _disposed = false; + private Singularity.SignalBag _signals = new Singularity.SignalBag(); private ulong[] _sig_app_sys = {}; private ulong[] _sig_home_cover = {}; private ulong _sig_wallpaper = 0; @@ -597,10 +598,10 @@ namespace Singularity { // Set tooltip with real-time shortcut var shortcuts = SystemMonitor.get_default().shortcuts; activities_btn.tooltip_text = shortcuts.tooltip_for(_("Overview"), "toggle_launcher"); - shortcuts.shortcut_changed.connect((action, accel) => { + _signals.add(shortcuts, shortcuts.shortcut_changed.connect((action, accel) => { if (action == "toggle_launcher") activities_btn.tooltip_text = shortcuts.tooltip_for(_("Overview"), "toggle_launcher"); - }); + })); var logo = new Image.from_icon_name("emblem-singularity"); var icon_theme = Gtk.IconTheme.get_for_display(Gdk.Display.get_default()); @@ -658,7 +659,7 @@ namespace Singularity { tiling_scroll_delta = 0; }); tiling_position.add_controller(tiling_scroll); - tiling.scrolling_position_changed.connect((monitor, position, + _signals.add(tiling, tiling.scrolling_position_changed.connect((monitor, position, visible_fraction, active) => { if (!active && (monitor == null || panel_monitor_matches(monitor))) { @@ -670,12 +671,12 @@ namespace Singularity { return; } update_tiling_position_visibility(); - }); - tiling.scrolling_drag_changed.connect((win, monitor, phase, + })); + _signals.add(tiling, tiling.scrolling_drag_changed.connect((win, monitor, phase, cursor_x, cursor_y) => { handle_tiling_drag(tiling, win, monitor, phase, cursor_x, cursor_y); - }); + })); tiling.refresh_scrolling_position(); } @@ -716,28 +717,28 @@ namespace Singularity { var network_icon = new Image.from_icon_name("network-wireless-symbolic"); network_icon.pixel_size = 16; network_icon.tooltip_text = network.wifi_ssid; - network.state_changed.connect(() => { + _signals.add(network, network.state_changed.connect(() => { network_icon.icon_name = network.wifi_icon; network_icon.tooltip_text = network.wifi_ssid; - }); + })); var audio = SystemMonitor.get_default().audio; var audio_icon = new Image.from_icon_name(audio.icon_name); audio_icon.pixel_size = 16; audio_icon.tooltip_text = "%d%%".printf((int)audio.volume); - audio.state_changed.connect(() => { + _signals.add(audio, audio.state_changed.connect(() => { audio_icon.icon_name = audio.icon_name; audio_icon.tooltip_text = "%d%%".printf((int)audio.volume); - }); + })); var hearing_icon = new Image.from_icon_name(VolumeOsd.HEARING_RISK_ICON); hearing_icon.pixel_size = 16; hearing_icon.add_css_class("warning"); hearing_icon.tooltip_text = _("High volume on headphones can damage your hearing"); hearing_icon.visible = audio.hearing_risk; - audio.state_changed.connect(() => { + _signals.add(audio, audio.state_changed.connect(() => { hearing_icon.visible = audio.hearing_risk; - }); + })); var battery_icon = new Image.from_icon_name("battery-full-symbolic"); battery_icon.pixel_size = 16; @@ -773,21 +774,21 @@ namespace Singularity { else bt_indicator.remove_css_class("calling"); } update_bt_indicator(); - bt.device_added.connect(() => update_bt_indicator()); - bt.device_removed.connect(() => update_bt_indicator()); - bt.device_changed.connect(() => update_bt_indicator()); - bt.state_changed.connect(() => update_bt_indicator()); - call_mon.changed.connect(() => update_bt_indicator()); + _signals.add(bt, bt.device_added.connect(() => update_bt_indicator())); + _signals.add(bt, bt.device_removed.connect(() => update_bt_indicator())); + _signals.add(bt, bt.device_changed.connect(() => update_bt_indicator())); + _signals.add(bt, bt.state_changed.connect(() => update_bt_indicator())); + _signals.add(call_mon, call_mon.changed.connect(() => update_bt_indicator())); sys_pill.append(bt_indicator); var vpn_indicator = new Image.from_icon_name("network-vpn-symbolic"); vpn_indicator.pixel_size = 16; vpn_indicator.visible = false; vpn_indicator.tooltip_text = ""; - network.vpn_state_changed.connect(() => { + _signals.add(network, network.vpn_state_changed.connect(() => { vpn_indicator.visible = network.vpn_active; vpn_indicator.tooltip_text = network.vpn_active ? _("VPN: %s").printf(network.vpn_name) : ""; - }); + })); sys_pill.prepend(vpn_indicator); sys_btn.set_child(sys_pill); sys_btn.clicked.connect(() => { @@ -804,13 +805,13 @@ namespace Singularity { battery_label.visible = show && power.is_present; } update_battery_label(); - power.state_changed.connect(() => { + _signals.add(power, power.state_changed.connect(() => { battery_icon.icon_name = power.icon_name; battery_icon.tooltip_text = "%d%%".printf((int)power.percentage); battery_icon.visible = power.is_present; update_battery_label(); - }); - _settings.changed["show-battery-percentage"].connect(() => update_battery_label()); + })); + _signals.add(_settings, _settings.changed["show-battery-percentage"].connect(() => update_battery_label())); var system_area = new Box(Orientation.HORIZONTAL, 4); system_area.valign = Align.CENTER; system_area.append(new SidebarWaitIndicator()); @@ -854,11 +855,11 @@ namespace Singularity { var nm = SystemMonitor.get_default().notifications; - nm.history_changed.connect(() => { + _signals.add(nm, nm.history_changed.connect(() => { notif_badge.visible = (nm.get_history().length() > 0); - }); + })); var focus = FocusManager.get_default(); - focus.changed.connect(() => FocusQuickTile.sync_indicator(notif_btn, notif_icon)); + _signals.add(focus, focus.changed.connect(() => FocusQuickTile.sync_indicator(notif_btn, notif_icon))); // Initial state notif_badge.visible = (nm.get_history().length() > 0); @@ -890,17 +891,17 @@ namespace Singularity { if (is_primary && !is_greeter_mode) layout_items["wallpaper"] = new WallpaperIndicator(_settings); reload_bar_layout(); - _settings.changed["panel-layout-left"].connect(() => { + _signals.add(_settings, _settings.changed["panel-layout-left"].connect(() => { if (!saving_bar_layout) reload_bar_layout(); - }); - _settings.changed["panel-layout-center"].connect(() => { + })); + _signals.add(_settings, _settings.changed["panel-layout-center"].connect(() => { if (!saving_bar_layout) reload_bar_layout(); - }); - _settings.changed["panel-layout-right"].connect(() => { + })); + _signals.add(_settings, _settings.changed["panel-layout-right"].connect(() => { if (!saving_bar_layout) reload_bar_layout(); - }); + })); if (_settings.settings_schema.has_key("show-wallpaper-controls")) { - _settings.changed["show-wallpaper-controls"].connect(() => reload_bar_layout()); + _signals.add(_settings, _settings.changed["show-wallpaper-controls"].connect(() => reload_bar_layout())); } if (!is_greeter_mode) { layout_editor = new BarLayoutEditController( @@ -913,39 +914,39 @@ namespace Singularity { { "overview", "workspaces", "tiling-position", "app-title", "global-menu", "system", "notifications", "clock", "wallpaper" }, { _("Overview"), _("Workspaces"), _("Scrolling Position"), _("App Title"), _("Global Menu"), _("System Status"), _("Notifications"), _("Clock"), _("Wallpaper") } ); - layout_editor.move_requested.connect((item_id, section, index) => { + _signals.add(layout_editor, layout_editor.move_requested.connect((item_id, section, index) => { if (bar_layout != null && bar_layout.move(item_id, section, index)) save_bar_layout(); - }); - layout_editor.edit_mode_changed.connect(() => { + })); + _signals.add(layout_editor, layout_editor.edit_mode_changed.connect(() => { update_visibility(); update_tiling_position_visibility(); - }); + })); } // Clock format from our own settings _clock_format_str = _settings.get_boolean("clock-use-12h") ? "%a, %b %e %I:%M %p" : "%a, %b %e %H:%M"; - _settings.changed["clock-use-12h"].connect(() => { + _signals.add(_settings, _settings.changed["clock-use-12h"].connect(() => { _clock_format_str = _settings.get_boolean("clock-use-12h") ? "%a, %b %e %I:%M %p" : "%a, %b %e %H:%M"; update_clock(); - }); + })); update_clock(); _sig_clock = SharedClock.get_default().minute_changed.connect(() => update_clock()); - _settings.changed["panel-fusion"].connect(() => { + _signals.add(_settings, _settings.changed["panel-fusion"].connect(() => { update_visibility(); - }); - _settings.changed["panel-flat"].connect(() => { + })); + _signals.add(_settings, _settings.changed["panel-flat"].connect(() => { update_flat_mode(); - }); - _settings.changed["panel-flat-opacity"].connect(() => { + })); + _signals.add(_settings, _settings.changed["panel-flat-opacity"].connect(() => { apply_flat_opacity(_settings); - }); + })); apply_flat_opacity(_settings); - _settings.changed["background-picture-uri"].connect(() => { + _signals.add(_settings, _settings.changed["background-picture-uri"].connect(() => { _last_strip_lum = -1.0; update_topbar_fg_class(); - }); + })); _sig_wallpaper = WallpaperManager.get_default().wallpaper_changed.connect(() => { _last_strip_lum = -1.0; update_topbar_fg_class(); @@ -1420,11 +1421,21 @@ namespace Singularity { public new void destroy() { teardown(); base.destroy(); + // Not inline: the caller may still be inside one of our handlers. + GLib.Idle.add(() => { + release_window_tree(this); + return GLib.Source.REMOVE; + }); + } + + ~Panel() { + debug("Panel released"); } private void teardown() { if (_disposed) return; _disposed = true; + _signals.clear(); foreach (ulong id in _sig_app_sys) GLib.SignalHandler.disconnect(AppSystem.get_default(), id); _sig_app_sys = {}; foreach (ulong id in _sig_home_cover) GLib.SignalHandler.disconnect(HomeCover.get_default(), id); diff --git a/src/core/layer_window.vala b/src/core/layer_window.vala index fee11ba5..d7f39b00 100644 --- a/src/core/layer_window.vala +++ b/src/core/layer_window.vala @@ -47,6 +47,88 @@ namespace Singularity { return true; } + /** + * Signal handlers a shell surface connected on objects that outlive it. + * + * A lambda that uses `this` holds a strong reference to its surface, so + * every one of them has to be disconnected before the surface can be + * released. Collect them here as they are connected and drop them all in + * the surface's teardown. + */ + public class SignalBag { + private struct Entry { + public Object emitter; + public ulong id; + } + + private Entry[] entries = {}; + + public void add (Object emitter, ulong id) { + if (id == 0) return; + entries += Entry () { emitter = emitter, id = id }; + } + + public void clear () { + foreach (var entry in entries) { + if (SignalHandler.is_connected (entry.emitter, entry.id)) + SignalHandler.disconnect (entry.emitter, entry.id); + } + entries = {}; + } + } + + private void _collect_tree (Gtk.Widget widget, GenericArray widgets, + GenericArray controllers) { + var list = widget.observe_controllers (); + for (uint i = 0; i < list.get_n_items (); i++) controllers.add (list.get_item (i)); + // A scrolled window keeps pointers to its own scrollbars and reads + // them when it is disposed, so only its content is ours to take apart. + var scrolled = widget as Gtk.ScrolledWindow; + if (scrolled != null) { + if (scrolled.child != null) { + widgets.add (scrolled.child); + _collect_tree (scrolled.child, widgets, controllers); + } + return; + } + for (var child = widget.get_first_child (); child != null; child = child.get_next_sibling ()) { + widgets.add (child); + _collect_tree (child, widgets, controllers); + } + } + + /** + * Dispose a destroyed shell surface and everything inside it. + * + * destroy() leaves the window alive (see window_was_destroyed). Its own + * widgets and event controllers carry lambdas that reference the window, + * and the window references them through its fields, so the loop never + * breaks on its own. Disposing each object drops its handlers, which + * releases the window once nothing outside still holds it. + * + * Parents go before their children, and a child a custom parent left + * attached is detached first: GTK requires a widget to have no parent by + * the time it is disposed. + */ + public void release_window_tree (Gtk.Window window) { + var widgets = new GenericArray (); + var controllers = new GenericArray (); + _collect_tree (window, widgets, controllers); + window.dispose (); + for (int i = 0; i < widgets.length; i++) { + // A scrolled window cannot be disposed twice, and it is disposed + // again when the last reference goes. Emptying it is enough here. + var scrolled = widgets[i] as Gtk.ScrolledWindow; + if (scrolled != null) { + scrolled.child = null; + continue; + } + if (widgets[i].get_parent () != null) widgets[i].unparent (); + widgets[i].dispose (); + } + for (int i = 0; i < controllers.length; i++) controllers[i].dispose (); + } + public void close_layer_window (Gtk.Window window) { ((Gtk.Widget) window).hide (); ((Gtk.Widget) window).unrealize ();