Skip to content

segfault while using Font object from a different init generation (2971) #1506

@GalacticEmperor1

Description

@GalacticEmperor1

Issue №2971 opened by chumpatrol1 at 2022-01-04 22:58:26

Environment:

You can get some of this info from the text that pops up in the console when you run a pygame program.

  • Operating system (e.g. Windows, Linux(Debian), Linux(Ubuntu), Mac): Windows 10
  • Python version (e.g. 3.7.9, 3.8.5) : 3.9.6/3.9.9
  • SDL version (e.g. SDL 2.0.12): 2.0.14/2.0.18
  • PyGame version (e.g. 2.0.0.dev10, 1.9.6): 2.0.1/2.1.2
  • Relevant hardware (e.g. if reporting a bug about a controller, tell us the brand & name of it):

Current behavior:

When I initialize a font (pg.font.Font) in the global namespace and try to use it in a local one (such as a function) through font.render, it causes a segmentation fault. If the font is initialized within the function itself (either by creating a local variable, or by creating a new key in a global dict) the program will not crash. In VSCode, no error shows up, but using my command prompt will show that there is a segmentation error with Pygame Parachute.

Expected behavior:

Pygame can use globally defined fonts without crashing

Steps to reproduce:

https://github.com/chumpatrol1/blob_ball/tree/global-font-crash-bug
Download and run. There is a readme which briefly describes the flow of my program.

Test code

If possible add a simple test program that shows the problem described in this report.
https://github.com/chumpatrol1/blob_ball/tree/global-font-crash-bug

pg.init()
menu_asset_cache = {
'ball': pg.transform.scale(pg.image.load(cwd + "/resources/images/balls/soccer_ball.png"), (76, 76)),
'font': pg.font.Font(str(p), 40),
'initialized': True,
}


def draw_main_menu(game_display, info_getter, settings):    
    if not menu_asset_cache['initialized']:
        menu_asset_cache['font'] = pg.font.Font(cwd + "/resources/fonts/neuropol-x-free.regular.ttf", 40)
        menu_asset_cache['initialized'] = True

    menu_font = menu_asset_cache['font']
    text_array = [
            menu_font.render('Play!', False, (0, 0, 150)),
            menu_font.render('Online', False, (0, 0, 150)),
            menu_font.render('Almanac', False, (0, 0, 150)),
            menu_font.render('Rules', False, (0, 0, 150)),
            menu_font.render('Settings', False, (0, 0, 150)),
            menu_font.render('Quit', False, (0, 0, 150))
        ]

Stack trace/error output/other error logs

C:\Users\Elijah McLaughlin\Desktop\Python Projects\Blob_Ball\blob_ball>Python main.py
pygame 2.1.2 (SDL 2.0.18, Python 3.9.6)
Hello from the pygame community. https://www.pygame.org/contribute.html
MAIN C:\Users\Elijah McLaughlin\Desktop\Python Projects\Blob_Ball\blob_ball
resources\fonts\neuropol-x-free.regular.ttf
<class 'pygame.font.Font'>
Fatal Python error: pygame_parachute: (pygame parachute) Segmentation Fault
Python runtime state: initialized

Current thread 0x00003028 (most recent call first):
  File "C:\Users\Elijah McLaughlin\Desktop\Python Projects\Blob_Ball\blob_ball\resources\graphics_engine\display_main_menu.py", line 31 in draw_main_menu
  File "C:\Users\Elijah McLaughlin\Desktop\Python Projects\Blob_Ball\blob_ball\resources\graphics_engine\display_graphics.py", line 68 in handle_graphics
  File "C:\Users\Elijah McLaughlin\Desktop\Python Projects\Blob_Ball\blob_ball\main.py", line 68 in display_graphics
  File "C:\Users\Elijah McLaughlin\Desktop\Python Projects\Blob_Ball\blob_ball\main.py", line 85 in run
  File "C:\Users\Elijah McLaughlin\Desktop\Python Projects\Blob_Ball\blob_ball\main.py", line 113 in <module>

C:\Users\Elijah McLaughlin\Desktop\Python Projects\Blob_Ball\blob_ball>

Comments

# # Starbuck5 commented at 2022-01-05 07:12:40

Thanks for making an issue report.

import pygame

pygame.init()

menu_asset_cache = {
'font': pygame.font.Font(None, 40),
}

def func():   
    menu_font = menu_asset_cache['font']
    menu_font.render('Play!', False, (0, 0, 150))

func()

Reducing this down to a minimum viable example doesn't segfault for me.


# # ankith26 commented at 2022-01-06 16:19:07

Ok I think I found out the true reason of the segfault. A font object created during the lifetime of another init/quit "generation", will segfault when it's used in a new "generation". Here is a minimal reproducer

import pygame
pygame.init()
font = pygame.font.Font(None, 40)
pygame.quit()
pygame.init()
surf = pygame.display.set_mode((400, 300))
surf.blit(font.render("hello", True, "white"))
pygame.time.delay(1000)
pygame.quit()

We had fixed a related issue recently, but seems like we have not handled all cases related to this issue. I wonder whether the same issue extends to mixer, freetype and other similar stuff.

But to fix OPs problem, it's a simple workaround, remove all the premature pg.quit() calls in your applications, and keep one only towards the end

We need to still investigate this issue regardless, if using font objects across different "generations" of pygame init lifecycle is not possible, we should atleast make it error cleanly with python exceptions and not have it segfault like this


# # oddbookworm commented at 2022-11-02 01:06:54

Can reproduce with pygame 2.1.3dev8 with python 3.10.7. Here is the gdb backtrace from the segfault

0x00007ffff6b96c19 in FT_Get_Char_Index () from /home/andrew/Envs/pygame_dev/lib/python3.10/site-packages/pygame/../pygame.libs/libfreetype-402bc0e3.so.6.18.3
(gdb) bt
# 0  0x00007ffff6b96c19 in FT_Get_Char_Index ()
   from /home/andrew/Envs/pygame_dev/lib/python3.10/site-packages/pygame/../pygame.libs/libfreetype-402bc0e3.so.6.18.3
# 1  0x00007ffff6d2420f in ?? ()
   from /home/andrew/Envs/pygame_dev/lib/python3.10/site-packages/pygame/../pygame.libs/libharfbuzz-f991c6ff.so.0.50100.0
# 2  0x00007ffff6d12828 in ?? ()
   from /home/andrew/Envs/pygame_dev/lib/python3.10/site-packages/pygame/../pygame.libs/libharfbuzz-f991c6ff.so.0.50100.0
# 3  0x00007ffff6d1565e in ?? ()
   from /home/andrew/Envs/pygame_dev/lib/python3.10/site-packages/pygame/../pygame.libs/libharfbuzz-f991c6ff.so.0.50100.0
# 4  0x00007ffff6d20504 in hb_shape_plan_execute ()
   from /home/andrew/Envs/pygame_dev/lib/python3.10/site-packages/pygame/../pygame.libs/libharfbuzz-f991c6ff.so.0.50100.0
# 5  0x00007ffff6d209a4 in hb_shape_full ()
   from /home/andrew/Envs/pygame_dev/lib/python3.10/site-packages/pygame/../pygame.libs/libharfbuzz-f991c6ff.so.0.50100.0
# 6  0x00007ffff6d71937 in ?? ()
   from /home/andrew/Envs/pygame_dev/lib/python3.10/site-packages/pygame/../pygame.libs/libSDL2_ttf-2-f8cb52bc.0.so.0.2000.1
# 7  0x00007ffff6d797ac in ?? ()
   from /home/andrew/Envs/pygame_dev/lib/python3.10/site-packages/pygame/../pygame.libs/libSDL2_ttf-2-f8cb52bc.0.so.0.2000.1
# 8  0x00007ffff6d8a803 in font_render ()
   from /home/andrew/Envs/pygame_dev/lib/python3.10/site-packages/pygame/font.cpython-310-x86_64-linux-gnu.so
# 9  0x00007ffff7d1d0d8 in ?? () from /usr/lib/libpython3.10.so.1.0
# 10 0x00007ffff7d225ba in _PyEval_EvalFrameDefault () from /usr/lib/libpython3.10.so.1.0
# 11 0x00007ffff7d20dd0 in ?? () from /usr/lib/libpython3.10.so.1.0
# 12 0x00007ffff7dcffb4 in PyEval_EvalCode () from /usr/lib/libpython3.10.so.1.0
# 13 0x00007ffff7ddf9d3 in ?? () from /usr/lib/libpython3.10.so.1.0
# 14 0x00007ffff7ddb36a in ?? () from /usr/lib/libpython3.10.so.1.0
# 15 0x00007ffff7c7e73c in ?? () from /usr/lib/libpython3.10.so.1.0
# 16 0x00007ffff7c7e3ed in _PyRun_SimpleFileObject () from /usr/lib/libpython3.10.so.1.0
# 17 0x00007ffff7c7eda0 in _PyRun_AnyFileObject () from /usr/lib/libpython3.10.so.1.0
# 18 0x00007ffff7dec2ad in Py_RunMain () from /usr/lib/libpython3.10.so.1.0
# 19 0x00007ffff7dc147b in Py_BytesMain () from /usr/lib/libpython3.10.so.1.0
--Type <RET> for more, q to quit, c to continue without paging--
# 20 0x00007ffff7a18290 in __libc_start_call_main (main=main@entry=0x555555555120, argc=argc@entry=2, argv=argv@entry=0x7fffffffd9d8)
    at ../sysdeps/nptl/libc_start_call_main.h:58
# 21 0x00007ffff7a1834a in __libc_start_main_impl (main=0x555555555120, argc=2, argv=0x7fffffffd9d8, init=<optimized out>, 
    fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffffd9c8) at ../csu/libc-start.c:381
# 22 0x0000555555555045 in _start ()

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugNot working as intendedfontpygame.font

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions