You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
When a thread containing SDL_CreateWindow() finishes its at_exit functions (i.e. thread->at_exit[i].pfn) may include pointers to functions which are no longer valid.
For example, client_thread_detach() is added to at_exit[].pfn by platform_tls_get(); however the calling thread exists after the EGL lib has been unloaded.
To reproduce
Compile and run the following demo code:
Also happens on Pi3A, PiZW and on Raspberry OS and Arch Linux.
Logs
Backtrace shows:
(gdb) bt
#0 0x758eb574 in ?? ()
#1 0x76a5a990 in vcos_dummy_thread_cleanup (cxt=0x76103480)
at /home/dom/projects/staging/userland/interface/vcos/pthreads/vcos_pthreads.c:104
#2 0x76e212fc in __nptl_deallocate_tsd () at pthread_create.c:301
#3 0x76e214a4 in start_thread (arg=0x76a56440) at pthread_create.c:497
Backtrace stopped: Cannot access memory at address 0x1fb509f8
Additional context
A limitation of SDL2 is that it must be initialised in the same thread as it waits for events.
Thus, we have a thread which has:
SDL_init();
...
SDL_CreateWindow(); // platform_tls_get() adds client_thread_detach as an at_exit function for thread
...
SDL_DestroyWindow(); // calls SDL_EGL_UnloadLibrary() which unloads client_thread_detach which is still in at_exit pfn list
...
SDL_Quit();
...
// thread ends, causing call to vcos_dummy_thread_cleanup() which calls at_exit pfn (i.e. client_thread_detach).
// This causes segmentation violation
I have a workaround in a forked repo which seems to fix issue. However I doubt that it is the right way to go. I can create a pull request if it helps.
The text was updated successfully, but these errors were encountered:
Don't you just call SDL_EGL_LoadLibrary(NULL) from the main application thread? SDL_CreateWindow only loads the library because you haven't already done so.
The only SDL_EGL_LoadLibrary() I see is extern int SDL_EGL_LoadLibrary(_THIS, const char *path, NativeDisplayType native_display, EGLenum platform); (where_THISis defined asSDL_VideoDevice *_this).
It assumes SDL has been initialised such that it dereferences _this without checking that it's been set.
Initialising SDL in main application is not feasible as the thread needs to call SDL_WaitEvent(); References: [1] , [2] , and others.
I think that this is because SDL video initialisation also initialises the Events subsystem.
Maybe I could duplicate the SDL code to load the EGL lib, but this is also far from ideal. Given the vast multitude of deployments of userland and SDL, it's highly unlikely that the issue is anything other than my code structure. I just haven't seen this problem mentioned anywhere else.
Describe the bug
When a thread containing SDL_CreateWindow() finishes its at_exit functions (i.e. thread->at_exit[i].pfn) may include pointers to functions which are no longer valid.
For example, client_thread_detach() is added to at_exit[].pfn by platform_tls_get(); however the calling thread exists after the EGL lib has been unloaded.
To reproduce
Compile and run the following demo code:
Build as follows:
Expected behaviour
Actual behaviour
System
raspinfo
Also happens on Pi3A, PiZW and on Raspberry OS and Arch Linux.
Logs
Backtrace shows:
Additional context
A limitation of SDL2 is that it must be initialised in the same thread as it waits for events.
Thus, we have a thread which has:
I have a workaround in a forked repo which seems to fix issue. However I doubt that it is the right way to go. I can create a pull request if it helps.
The text was updated successfully, but these errors were encountered: