Friday, November 27, 2020

Mystery crash in an SDL2 application (solved)

I am developing an application in SDL2. It works OK most of the time. But once in a while, it will crash while just going to refresh the screen with new content with SDL_RenderPresent(). The backtrace from gdb shows:

#0  0x00007ffff73a1c7b in raise () from /lib64/libc.so.6
#1  0x00007ffff7382548 in abort () from /lib64/libc.so.6       
#2  0x00007ffff35a682b in ?? () from /usr/lib64/xorg/modules/dri/iris_dri.so
#3  0x00007ffff40a5886 in ?? () from /usr/lib64/xorg/modules/dri/iris_dri.so
#4  0x00007ffff35c29e9 in ?? () from /usr/lib64/xorg/modules/dri/iris_dri.so
#5  0x00007ffff35b6d2b in ?? () from /usr/lib64/xorg/modules/dri/iris_dri.so
#6  0x00007ffff4ef99e0 in glPrimitiveBoundingBox () from /usr/lib64/libGLX_mesa.so.0
#7  0x00007ffff4eecad1 in ?? () from /usr/lib64/libGLX_mesa.so.0
#8  0x00007ffff7ef6283 in ?? () from /usr/lib64/libSDL2-2.0.so.0
#9  0x0000000000415f42 in refresh_display (ctx=0x7fffffffd370) at gui/main.c:1077
#10 0x0000000000413c79 in main_loop_handler (ctx=0x7fffffffd370) at gui/main.c:362
#11 0x0000000000413b22 in main (argc=1, argv=0x7fffffffdd58) at gui/main.c:323

I searched around the web with SDL2 crashing during SDL_RenderPresent(). Most of them points to some form of memory corruption causing that, but I have double check my code with various tools and manual inspection, there is no memory corruption. Then I came across this article "Rock solid frame rates with SDL2", it mentioned a function SDL_SetSwapInterval() which will delay SDL_RenderPresent() from swapping the render buffers till the swap interval (wait for vsync). This seems match what I suspected that the buffer is updated on the wrong time and causing the crash.

There is just a little problem, there is no more SDL_SetSwapInterval() any more, in stead it become SDL_GL_SetSwapInterval(). I added in the oneline call SDL_GL_SetSwapInterval(1), and vala, so far all tests do not show the crash anymore.

No comments: