From bfd014733b6a377e0f88d4e928fea16b582d1b3a Mon Sep 17 00:00:00 2001 From: Starbuck5 <46412508+Starbuck5@users.noreply.github.com> Date: Sun, 3 Aug 2025 22:54:17 -0700 Subject: [PATCH] Fix font test segfault on SDL3 --- src_c/font.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src_c/font.c b/src_c/font.c index 3f46279511..11b7b20530 100644 --- a/src_c/font.c +++ b/src_c/font.c @@ -1210,6 +1210,17 @@ font_dealloc(PyFontObject *self) { TTF_Font *font = PyFont_AsFont(self); if (font && font_initialized) { + // In SDL3_ttf, it seems that closing a font after its library was + // destroyed segfaults. So only close if same generation. + // TODO SDL3: + // TTF docs say "A well-written program should call TTF_CloseFont() + // on any open fonts before calling this function!" +#if SDL_VERSION_ATLEAST(3, 0, 0) + if (self->ttf_init_generation == current_ttf_generation) { + TTF_CloseFont(font); + } + self->font = NULL; +#else if (self->ttf_init_generation != current_ttf_generation) { // Since TTF_Font is a private structure // it's impossible to access face field in a common way. @@ -1218,6 +1229,7 @@ font_dealloc(PyFontObject *self) } TTF_CloseFont(font); self->font = NULL; +#endif } if (self->weakreflist) {