From f710781b1643d76799db175347ee096cbbba62e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pa=CC=84vels=20Nadtoc=CC=8Cajevs?= <7645683+bruvzg@users.noreply.github.com> Date: Tue, 18 Feb 2025 15:16:51 +0200 Subject: [PATCH] Prevent pending input event callbacks from erasing the window in the middle of a loop. --- platform/linuxbsd/x11/display_server_x11.cpp | 8 ++++++-- platform/macos/display_server_macos.mm | 8 ++++++-- platform/windows/display_server_windows.cpp | 12 ++++++++---- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/platform/linuxbsd/x11/display_server_x11.cpp b/platform/linuxbsd/x11/display_server_x11.cpp index e35b6e84e2e..a186c83e369 100644 --- a/platform/linuxbsd/x11/display_server_x11.cpp +++ b/platform/linuxbsd/x11/display_server_x11.cpp @@ -4227,13 +4227,17 @@ void DisplayServerX11::_dispatch_input_event(const Ref &p_event) { } } } else { - // Send to all windows. + // Send to all windows. Copy all pending callbacks, since callback can erase window. + Vector cbs; for (KeyValue &E : windows) { Callable callable = E.value.input_event_callback; if (callable.is_valid()) { - callable.call(p_event); + cbs.push_back(callable); } } + for (const Callable &cb : cbs) { + cb.call(p_event); + } } } diff --git a/platform/macos/display_server_macos.mm b/platform/macos/display_server_macos.mm index bd6d435f83d..346faef842a 100644 --- a/platform/macos/display_server_macos.mm +++ b/platform/macos/display_server_macos.mm @@ -411,13 +411,17 @@ void DisplayServerMacOS::_dispatch_input_event(const Ref &p_event) { } } } else { - // Send to all windows. + // Send to all windows. Copy all pending callbacks, since callback can erase window. + Vector cbs; for (KeyValue &E : windows) { Callable callable = E.value.input_event_callback; if (callable.is_valid()) { - callable.call(p_event); + cbs.push_back(callable); } } + for (const Callable &cb : cbs) { + cb.call(p_event); + } } in_dispatch_input_event = false; } diff --git a/platform/windows/display_server_windows.cpp b/platform/windows/display_server_windows.cpp index 4479fda54a9..8609a6b4cd8 100644 --- a/platform/windows/display_server_windows.cpp +++ b/platform/windows/display_server_windows.cpp @@ -4253,13 +4253,17 @@ void DisplayServerWindows::_dispatch_input_event(const Ref &p_event) } } } else { - // Send to all windows. - for (const KeyValue &E : windows) { - const Callable callable = E.value.input_event_callback; + // Send to all windows. Copy all pending callbacks, since callback can erase window. + Vector cbs; + for (KeyValue &E : windows) { + Callable callable = E.value.input_event_callback; if (callable.is_valid()) { - callable.call(p_event); + cbs.push_back(callable); } } + for (const Callable &cb : cbs) { + cb.call(p_event); + } } in_dispatch_input_event = false;