Skip to content

Commit 9734b92

Browse files
authored
Merge pull request #3549 from Starbuck5/fix-font-test-segfault-sdl3
Fix font test segfault on SDL3
2 parents bf90331 + bfd0147 commit 9734b92

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src_c/font.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,17 @@ font_dealloc(PyFontObject *self)
12101210
{
12111211
TTF_Font *font = PyFont_AsFont(self);
12121212
if (font && font_initialized) {
1213+
// In SDL3_ttf, it seems that closing a font after its library was
1214+
// destroyed segfaults. So only close if same generation.
1215+
// TODO SDL3:
1216+
// TTF docs say "A well-written program should call TTF_CloseFont()
1217+
// on any open fonts before calling this function!"
1218+
#if SDL_VERSION_ATLEAST(3, 0, 0)
1219+
if (self->ttf_init_generation == current_ttf_generation) {
1220+
TTF_CloseFont(font);
1221+
}
1222+
self->font = NULL;
1223+
#else
12131224
if (self->ttf_init_generation != current_ttf_generation) {
12141225
// Since TTF_Font is a private structure
12151226
// it's impossible to access face field in a common way.
@@ -1218,6 +1229,7 @@ font_dealloc(PyFontObject *self)
12181229
}
12191230
TTF_CloseFont(font);
12201231
self->font = NULL;
1232+
#endif
12211233
}
12221234

12231235
if (self->weakreflist) {

0 commit comments

Comments
 (0)