Skip to content

Fix display.set_mode segfault after resizing #3501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src_c/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,19 @@ _get_video_window_pos(int *x, int *y, int *center_window)
return 0;
}

static void
_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;
}
}

static int SDLCALL
pg_ResizeEventWatch(void *userdata, SDL_Event *event)
{
Expand Down Expand Up @@ -786,11 +799,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, 1);
}
return 0;
}
Expand Down Expand Up @@ -1374,6 +1383,7 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
surface = pgSurface_New2(surf, newownedsurf != NULL);
}
else {
_check_window_resized(win, 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm probably failing to notice something here, but why aren't we freeing the old surface here?

Copy link
Member Author

@MightyJosip MightyJosip Jun 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because if we free then we get segmentation fault from the linked issue. You have more explanation in the discord link

pgSurface_SetSurface(surface, surf, newownedsurf != NULL);
Py_INCREF(surface);
}
Expand Down
Loading