From 1bc3a5a12329a5d9ef55d7b9ace6c04dc88a9da5 Mon Sep 17 00:00:00 2001 From: j4kuuu Date: Wed, 11 Nov 2026 16:20:14 +0100 Subject: [PATCH] core: release client proxies before the display disconnects apps hold their windows and outputs across enterLoop() and drop them after it returns, but cleanup() has already disconnected the display by then, so every proxy they still own gets destroyed on freed memory. since #120 a lost compositor ends the loop instead of aborting, so every toolkit app now crashes this way at session end (hyprpaper in ~CWaylandOutput, layers, lock surfaces). drop every window, layer, lock surface, popup and output proxy at the start of cleanup(), and let a popup be destroyed after the backend is gone. fixes #134 --- src/core/Backend.cpp | 3 +++ src/core/platforms/WaylandPlatform.cpp | 22 ++++++++++++++++++++++ src/core/platforms/WaylandPlatform.hpp | 1 + src/window/IWaylandWindow.cpp | 14 ++++++++++++++ src/window/IWaylandWindow.hpp | 1 + src/window/WaylandPopup.cpp | 4 +++- 6 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/core/Backend.cpp b/src/core/Backend.cpp index d9218469..a8501036 100644 --- a/src/core/Backend.cpp +++ b/src/core/Backend.cpp @@ -664,6 +664,9 @@ void CBackend::cleanup() { if (m_cleaned.exchange(true)) return; + // apps drop their windows and outputs after enterLoop() returns, past the disconnect below + g_waylandPlatform->releaseProxies(); + g_asyncResourceGatherer.reset(); m_waylandPostDispatch.reset(); diff --git a/src/core/platforms/WaylandPlatform.cpp b/src/core/platforms/WaylandPlatform.cpp index e93ec47e..72446e5b 100644 --- a/src/core/platforms/WaylandPlatform.cpp +++ b/src/core/platforms/WaylandPlatform.cpp @@ -176,6 +176,28 @@ CWaylandPlatform::~CWaylandPlatform() { wl_display_disconnect(DPY); } +void CWaylandPlatform::releaseProxies() { + // by value: a release can drop the last ref and the destructor erases from the list + const auto release = [](auto windows) { + for (const auto& w : windows) { + if (const auto window = w.lock()) + window->releaseWayland(); + } + }; + + release(m_windows); + release(m_layers); + + if (const auto lock = m_sessionLockState.lock()) { + release(lock->m_lockSurfaces); + lock->m_lock.reset(); + } + + for (const auto& o : m_outputs) { + o->m_wlOutput.reset(); + } +} + bool CWaylandPlatform::dispatchEvents() { int ret = 0; do { diff --git a/src/core/platforms/WaylandPlatform.hpp b/src/core/platforms/WaylandPlatform.hpp index 06fd2932..35f7616b 100644 --- a/src/core/platforms/WaylandPlatform.hpp +++ b/src/core/platforms/WaylandPlatform.hpp @@ -54,6 +54,7 @@ namespace Hyprtoolkit { void setCursor(ePointerShape shape); bool dispatchEvents(); + void releaseProxies(); void setClipboard(const std::string& text); std::string readClipboard(); diff --git a/src/window/IWaylandWindow.cpp b/src/window/IWaylandWindow.cpp index 33ea5e33..edda939d 100644 --- a/src/window/IWaylandWindow.cpp +++ b/src/window/IWaylandWindow.cpp @@ -246,6 +246,20 @@ SP IWaylandWindow::openPopup(const SWindowCreationData& data) { return x; } +void IWaylandWindow::releaseWayland() { + // popup close() erases itself from m_popups + const auto popups = m_popups; + for (const auto& p : popups) { + if (const auto popup = reinterpretPointerCast(p.lock())) + popup->releaseWayland(); + } + + close(); + + // layers and lock surfaces keep their wl_surface across close() for a reopen + m_waylandState = {}; +} + void IWaylandWindow::mouseButton(const Input::eMouseButton button, bool state) { if (m_popups.empty() || !state || m_ignoreNextButtonEvent) { m_ignoreNextButtonEvent = false; diff --git a/src/window/IWaylandWindow.hpp b/src/window/IWaylandWindow.hpp index 0bbb4253..5ae990e0 100644 --- a/src/window/IWaylandWindow.hpp +++ b/src/window/IWaylandWindow.hpp @@ -56,6 +56,7 @@ namespace Hyprtoolkit { virtual void setIMTo(const Hyprutils::Math::CBox& box, const std::string& str, size_t cursor); virtual void resetIM(); virtual void onPreRender(); + void releaseWayland(); std::vector> m_popups; diff --git a/src/window/WaylandPopup.cpp b/src/window/WaylandPopup.cpp index 74fd7fec..4cde5c3c 100644 --- a/src/window/WaylandPopup.cpp +++ b/src/window/WaylandPopup.cpp @@ -117,7 +117,8 @@ void CWaylandPopup::open() { } void CWaylandPopup::close() { - if (g_waylandPlatform->m_currentWindow == m_self) + // the app can drop a popup after the backend is gone + if (g_waylandPlatform && g_waylandPlatform->m_currentWindow == m_self) g_waylandPlatform->m_currentWindow = m_parent; if (m_parent) @@ -138,6 +139,7 @@ void CWaylandPopup::close() { m_waylandState.surface->sendDestroy(); m_waylandState = {}; + m_wlPopupState = {}; m_events.popupClosed.emit(); }