Skip to content

Commit 0ca2d18

Browse files
committed
Make convert format hold its own PixelFormat struct
I'm worried that if we use a reference to a pixelformat struct from a surface, the pixelformat might be deallocated later by SDL. This makes the code more complex but it ensures that the module always has a handle on a real and not-deallocated pixelformat.
1 parent bdb0776 commit 0ca2d18

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

src_c/base.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,10 +2068,14 @@ pg_GetDefaultConvertFormat(void)
20682068
return pg_default_convert_format;
20692069
}
20702070

2071-
static void
2072-
pg_SetDefaultConvertFormat(SDL_PixelFormat *fmt)
2071+
static SDL_PixelFormat *
2072+
pg_SetDefaultConvertFormat(Uint32 format)
20732073
{
2074-
pg_default_convert_format = fmt;
2074+
if (pg_default_convert_format != NULL) {
2075+
SDL_FreeFormat(pg_default_convert_format);
2076+
}
2077+
pg_default_convert_format = SDL_AllocFormat(format);
2078+
return pg_default_convert_format; // returns for NULL error checking
20752079
}
20762080

20772081
static char *

src_c/include/_pygame.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ typedef struct pg_bufferinfo_s {
177177
(*(SDL_PixelFormat * (*)(void)) PYGAMEAPI_GET_SLOT(base, 27))
178178

179179
#define pg_SetDefaultConvertFormat \
180-
(*(void (*)(SDL_PixelFormat *))PYGAMEAPI_GET_SLOT(base, 28))
180+
(*(SDL_PixelFormat * (*)(Uint32)) PYGAMEAPI_GET_SLOT(base, 28))
181181

182182
#define import_pygame_base() IMPORT_PYGAME_MODULE(base)
183183
#endif /* ~PYGAMEAPI_BASE_INTERNAL */

src_c/window.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,13 @@ window_get_surface(pgWindowObject *self)
164164
}
165165
self->surf->surf = _surf;
166166

167-
if (pg_GetDefaultConvertFormat() == NULL)
168-
pg_SetDefaultConvertFormat(_surf->format);
167+
if (pg_GetDefaultConvertFormat() == NULL) {
168+
if (pg_SetDefaultConvertFormat(_surf->format->format) == NULL) {
169+
/* This is very unlikely, I think only would happen if SDL runs
170+
* out of memory when allocating the format. */
171+
return RAISE(pgExc_SDLError, SDL_GetError());
172+
}
173+
}
169174

170175
Py_INCREF(self->surf);
171176
return (PyObject *)self->surf;

0 commit comments

Comments
 (0)