From 59e8ab321e181ad6fa3933131c997a52996057b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josip=20Komljenovi=C4=87?= Date: Sun, 15 Jun 2025 12:40:57 +0200 Subject: [PATCH 1/2] Fix display.set_mode segfault after resizing --- src_c/display.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src_c/display.c b/src_c/display.c index 8f72dcbcc6..4144515a30 100644 --- a/src_c/display.c +++ b/src_c/display.c @@ -707,6 +707,16 @@ _get_video_window_pos(int *x, int *y, int *center_window) return 0; } +static void +_check_window_resized(SDL_Window *window) +{ + SDL_Surface *sdl_surface = SDL_GetWindowSurface(window); + pgSurfaceObject *old_surface = pg_GetDefaultWindowSurface(); + if (sdl_surface != old_surface->surf) { + old_surface->surf = sdl_surface; + } +} + static int SDLCALL pg_ResizeEventWatch(void *userdata, SDL_Event *event) { @@ -786,11 +796,7 @@ pg_ResizeEventWatch(void *userdata, SDL_Event *event) } if (event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { - SDL_Surface *sdl_surface = SDL_GetWindowSurface(window); - pgSurfaceObject *old_surface = pg_GetDefaultWindowSurface(); - if (sdl_surface != old_surface->surf) { - old_surface->surf = sdl_surface; - } + _check_window_resized(window); } return 0; } @@ -1374,6 +1380,7 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds) surface = pgSurface_New2(surf, newownedsurf != NULL); } else { + _check_window_resized(win); pgSurface_SetSurface(surface, surf, newownedsurf != NULL); Py_INCREF(surface); } From 2f9ebbe7a2a49d5a486c79b915580254847551b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josip=20Komljenovi=C4=87?= Date: Mon, 16 Jun 2025 09:07:45 +0200 Subject: [PATCH 2/2] Fix display.set_mode segfault after resizing --- src_c/display.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src_c/display.c b/src_c/display.c index 4144515a30..6418b58c75 100644 --- a/src_c/display.c +++ b/src_c/display.c @@ -708,11 +708,14 @@ _get_video_window_pos(int *x, int *y, int *center_window) } static void -_check_window_resized(SDL_Window *window) +_check_window_resized(SDL_Window *window, int free_old_surf) { SDL_Surface *sdl_surface = SDL_GetWindowSurface(window); pgSurfaceObject *old_surface = pg_GetDefaultWindowSurface(); if (sdl_surface != old_surface->surf) { + if (free_old_surf) { + SDL_FreeSurface(old_surface->surf); + } old_surface->surf = sdl_surface; } } @@ -796,7 +799,7 @@ pg_ResizeEventWatch(void *userdata, SDL_Event *event) } if (event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { - _check_window_resized(window); + _check_window_resized(window, 1); } return 0; } @@ -1380,7 +1383,7 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds) surface = pgSurface_New2(surf, newownedsurf != NULL); } else { - _check_window_resized(win); + _check_window_resized(win, 0); pgSurface_SetSurface(surface, surf, newownedsurf != NULL); Py_INCREF(surface); }