Skip to content
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

Fix segmentation fault when destroying window #2530

Merged
merged 4 commits into from
Oct 31, 2023
Merged
Changes from 2 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
30 changes: 30 additions & 0 deletions src_c/surface.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ static SDL_Surface *
pg_DisplayFormat(SDL_Surface *surface);
static int
_PgSurface_SrcAlpha(SDL_Surface *surf);
static int
_is_pg_window_active();

#if !SDL_VERSION_ATLEAST(2, 0, 10)
static Uint32
Expand Down Expand Up @@ -1767,6 +1769,9 @@ surf_get_clip(PyObject *self, PyObject *_null)
static PyObject *
surf_fill(pgSurfaceObject *self, PyObject *args, PyObject *keywds)
{
if (!_is_pg_window_active())
return RAISE(PyExc_RuntimeError, "pygame display window is not active");

SDL_Surface *surf = pgSurface_AsSurface(self);
SDL_Rect *rect, temp;
PyObject *r = NULL;
Expand Down Expand Up @@ -1855,6 +1860,9 @@ surf_fill(pgSurfaceObject *self, PyObject *args, PyObject *keywds)
static PyObject *
surf_blit(pgSurfaceObject *self, PyObject *args, PyObject *keywds)
{
if (!_is_pg_window_active())
return RAISE(PyExc_RuntimeError, "pygame display window is not active");

SDL_Surface *src, *dest = pgSurface_AsSurface(self);
SDL_Rect *src_rect, temp;
PyObject *argpos, *argrect = NULL;
Expand Down Expand Up @@ -1927,6 +1935,9 @@ surf_blit(pgSurfaceObject *self, PyObject *args, PyObject *keywds)
static PyObject *
surf_blits(pgSurfaceObject *self, PyObject *args, PyObject *keywds)
{
if (!_is_pg_window_active())
return RAISE(PyExc_RuntimeError, "pygame display window is not active");

SDL_Surface *src, *dest = pgSurface_AsSurface(self);
SDL_Rect *src_rect, temp;
PyObject *srcobject = NULL, *argpos = NULL, *argrect = NULL;
Expand Down Expand Up @@ -2169,6 +2180,9 @@ surf_blits(pgSurfaceObject *self, PyObject *args, PyObject *keywds)
static PyObject *
surf_fblits(pgSurfaceObject *self, PyObject *const *args, Py_ssize_t nargs)
{
if (!_is_pg_window_active())
return RAISE(PyExc_RuntimeError, "pygame display window is not active");

SDL_Surface *src, *dest = pgSurface_AsSurface(self);
SURF_INIT_CHECK(dest)

Expand Down Expand Up @@ -2444,6 +2458,21 @@ _PgSurface_SrcAlpha(SDL_Surface *surf)
return (mode != SDL_BLENDMODE_NONE);
}

/*
* Returns 1 if active, 0 if not.
* See: https://github.com/pygame-community/pygame-ce/issues/2523
*/
static int
_is_pg_window_active()
{
SDL_Surface *win_surface;
if (!SDL_GetWindowSurface(&win_surface)) {
return 0;
}

return 1;
}

static PyObject *
surf_get_flags(PyObject *self, PyObject *_null)
{
Expand Down Expand Up @@ -4073,3 +4102,4 @@ MODINIT_DEFINE(surface)
}
return module;
}

Loading