From 75eacb95ef8448b094a1b89d4ba39bf8fdbd72de Mon Sep 17 00:00:00 2001 From: Lukas Fleischer Date: Mon, 22 May 2023 10:02:14 -0400 Subject: [PATCH] Fix SEGFAULT in battery module In waybar::modules::Battery::~Battery(), store a copy of the batteries_ iterator before calling erase(), as erase() invalidates the iterator. Prior to this change, disconnecting outputs resulted in a SEGFAULT when using the battery module; e.g., [debug] Received SIGCHLD in signalThread [debug] Cmd exited with code 0 [debug] Received SIGCHLD in signalThread [debug] Cmd exited with code 0 [debug] Received SIGCHLD in signalThread [debug] Cmd exited with code 0 [debug] Output removed: AU Optronics 0x2336 [info] Bar configured (width: 1280, height: 25) for output: eDP-1 [info] Bar configured (width: 1280, height: 25) for output: eDP-1 zsh: segmentation fault (core dumped) ./build/waybar -l trace Signed-off-by: Lukas Fleischer --- src/modules/battery.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index abd1240c..c0f433ae 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -36,7 +36,8 @@ waybar::modules::Battery::~Battery() { } close(global_watch_fd_); - for (auto it = batteries_.cbegin(); it != batteries_.cend(); it++) { + for (auto it = batteries_.cbegin(), next_it = it; it != batteries_.cend(); it = next_it) { + ++next_it; auto watch_id = (*it).second; if (watch_id >= 0) { inotify_rm_watch(battery_watch_fd_, watch_id);