video: Handle window destruction in event handlers

Windows may be destroyed in event handlers, so check the window validity after pushing window events onto the queue to ensure that the window and its properties are still valid.
This commit is contained in:
Frank Praznik
2025-12-09 12:35:37 -05:00
parent 62639fdf88
commit 5b572638b8

View File

@@ -252,6 +252,10 @@ bool SDL_SendWindowEvent(SDL_Window *window, SDL_EventType windowevent, int data
posted = SDL_PushEvent(&event);
}
// Ensure that the window is still valid, as it may have been destroyed in an event handler.
window = SDL_GetWindowFromID(event.window.windowID);
if (window) {
switch (windowevent) {
case SDL_EVENT_WINDOW_SHOWN:
SDL_OnWindowShown(window);
@@ -295,19 +299,19 @@ bool SDL_SendWindowEvent(SDL_Window *window, SDL_EventType windowevent, int data
default:
break;
}
}
if (windowevent == SDL_EVENT_WINDOW_CLOSE_REQUESTED && !window->parent && !SDL_HasActiveTrays()) {
int toplevel_count = 0;
SDL_Window *n;
for (n = _this->windows; n; n = n->next) {
if (windowevent == SDL_EVENT_WINDOW_CLOSE_REQUESTED && !SDL_HasActiveTrays()) {
int count = window ? 0 : 1;
for (SDL_Window *n = _this->windows; n; n = n->next) {
if (!n->parent && !(n->flags & SDL_WINDOW_HIDDEN)) {
++toplevel_count;
++count;
}
}
if (toplevel_count <= 1) {
if (count <= 1) {
if (SDL_GetHintBoolean(SDL_HINT_QUIT_ON_LAST_WINDOW_CLOSE, true)) {
SDL_SendQuit(); // This is the last toplevel window in the list so send the SDL_EVENT_QUIT event
SDL_SendQuit(); // This is the last window in the list, so send the SDL_EVENT_QUIT event
}
}
}