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 68e7351762b..2a49bb9da57 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;