From 943ba3a2da2730d536a2f6409dfe61a8523239a6 Mon Sep 17 00:00:00 2001 From: Aleksei Bavshin Date: Tue, 9 Feb 2021 19:24:46 -0800 Subject: [PATCH] fix: schedule output destruction on idle callback Defer destruction of bars for the output to the next iteration of the event loop to avoid deleting objects referenced by currently executed code. --- include/client.hpp | 1 + src/client.cpp | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/include/client.hpp b/include/client.hpp index 5965f7cd..ec3866ad 100644 --- a/include/client.hpp +++ b/include/client.hpp @@ -51,6 +51,7 @@ class Client { static void handleOutputDescription(void *, struct zxdg_output_v1 *, const char *); void handleMonitorAdded(Glib::RefPtr monitor); void handleMonitorRemoved(Glib::RefPtr monitor); + void handleDeferredMonitorRemoval(Glib::RefPtr monitor); Json::Value config_; Glib::RefPtr style_context_; diff --git a/src/client.cpp b/src/client.cpp index fcfcd98c..1c48c813 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -179,6 +179,16 @@ void waybar::Client::handleMonitorAdded(Glib::RefPtr monitor) { void waybar::Client::handleMonitorRemoved(Glib::RefPtr monitor) { spdlog::debug("Output removed: {} {}", monitor->get_manufacturer(), monitor->get_model()); + /* This event can be triggered from wl_display_roundtrip called by GTK or our code. + * Defer destruction of bars for the output to the next iteration of the event loop to avoid + * deleting objects referenced by currently executed code. + */ + Glib::signal_idle().connect_once( + sigc::bind(sigc::mem_fun(*this, &Client::handleDeferredMonitorRemoval), monitor), + Glib::PRIORITY_HIGH_IDLE); +} + +void waybar::Client::handleDeferredMonitorRemoval(Glib::RefPtr monitor) { for (auto it = bars.begin(); it != bars.end();) { if ((*it)->output->monitor == monitor) { auto output_name = (*it)->output->name;