Crash when calling SDL_PollEvent() on ios

Hello,

I’m having a persistent crash on my sdl2 aplication built for ios, and I’m having trouble finding out the cause. It happens in about 1.5% of sessions.

Here is what xcode shows:

And here is one of the .crash files generated(I cannot attach the file since I’m new here):

Incident Identifier: 03BD7821-B081-451E-8A06-55BD91C659D4
Distributor ID:      com.apple.AppStore
Hardware Model:      iPhone13,3
Process:             Baseball '97 [26282]
Path:                /private/var/containers/Bundle/Application/425699E9-FAE6-41FF-A570-B2F57135925F/Baseball '97.app/Baseball '97
Identifier:          com.****.****
Version:             1.1.4 (5)
AppStoreTools:       16E137
AppVariant:          1:iPhone13,3:16
Code Type:           ARM-64 (Native)
Role:                Foreground
Parent Process:      launchd [1]
Coalition:           com.****.**** [1982]

Date/Time:           2025-05-07 00:12:09.9261 -0400
Launch Time:         2025-05-06 00:12:17.1639 -0400
OS Version:          iPhone OS 17.6.1 (21G93)
Release Type:        User
Baseband Version:    4.70.01
Report Version:      104

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Termination Reason: SIGNAL 6 Abort trap: 6
Terminating Process: Baseball '97 [26282]

Triggered by Thread:  0


Thread 0 name:
Thread 0 Crashed:
0   libsystem_kernel.dylib        	0x00000001e9b5c2ec __pthread_kill + 8 (:-1)
1   libsystem_pthread.dylib       	0x00000001fd94fc0c pthread_kill + 268 (pthread.c:1721)
2   libsystem_c.dylib             	0x00000001a8e5bba0 abort + 180 (abort.c:118)
3   libsystem_malloc.dylib        	0x00000001b109f588 malloc_vreport + 896 (malloc_printf.c:251)
4   libsystem_malloc.dylib        	0x00000001b109c430 malloc_zone_error + 104 (malloc_printf.c:319)
5   libsystem_malloc.dylib        	0x00000001b10bb508 nanov2_guard_corruption_detected + 44 (nanov2_malloc.c:2425)
6   libsystem_malloc.dylib        	0x00000001b10a32e4 nanov2_allocate_outlined + 404 (nanov2_malloc.c:2918)
7   CoreFoundation                	0x00000001a0ecbc3c CFRunLoopRunSpecific + 452 (CFRunLoop.c:3406)
8   SDL2                          	0x00000001058c1424 UIKit_PumpEvents + 64 (SDL_uikitevents.m:140)
9   SDL2                          	0x00000001058f25b4 SDL_PumpEventsInternal + 64 (SDL_events.c:918)
10  SDL2                          	0x00000001058f26e4 SDL_WaitEventTimeout + 88 (SDL_events.c:1093)
11  Baseball '97                  	0x0000000104f7943c PM_HandleWindowsEventsLow(int) + 64 (PMAINSDL.cpp:1154)
12  Baseball '97                  	0x0000000104f791a8 SDL_main + 800 (PMAINSDL.cpp:728)
13  SDL2                          	0x000000010593e794 -[SDLUIKitDelegate postFinishLaunch] + 64 (SDL_uikitappdelegate.m:399)
14  Foundation                    	0x000000019fdd7818 __NSFireDelayedPerform + 372 (NSRunLoop.m:646)
15  CoreFoundation                	0x00000001a0f30a2c __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 32 (CFRunLoop.c:1797)
16  CoreFoundation                	0x00000001a0f306d0 __CFRunLoopDoTimer + 1004 (CFRunLoop.c:2404)
17  CoreFoundation                	0x00000001a0f3022c __CFRunLoopDoTimers + 288 (CFRunLoop.c:2562)
18  CoreFoundation                	0x00000001a0ecc888 __CFRunLoopRun + 1856 (CFRunLoop.c:3122)
19  CoreFoundation                	0x00000001a0ecbcd8 CFRunLoopRunSpecific + 608 (CFRunLoop.c:3420)
20  GraphicsServices              	0x00000001e59191a8 GSEventRunModal + 164 (GSEvent.c:2196)
21  UIKitCore                     	0x00000001a3505ae8 -[UIApplication _run] + 888 (UIApplication.m:3713)
22  UIKitCore                     	0x00000001a35b9d98 UIApplicationMain + 340 (UIApplication.m:5303)
23  SDL2                          	0x000000010593d8e0 SDL_UIKitRunApp + 196 (SDL_uikitappdelegate.m:69)
24  dyld                          	0x00000001c46a3154 start + 2356 (dyldMain.cpp:1298)

Thread 1:
0   libsystem_pthread.dylib       	0x00000001fd9490c4 start_wqthread + 0 (:-1)

Thread 2:
0   libsystem_pthread.dylib       	0x00000001fd9490c4 start_wqthread + 0 (:-1)

Thread 3:
0   libsystem_pthread.dylib       	0x00000001fd9490c4 start_wqthread + 0 (:-1)

Thread 4 name:
Thread 4:
0   libsystem_kernel.dylib        	0x00000001e9b516c8 mach_msg2_trap + 8 (:-1)
1   libsystem_kernel.dylib        	0x00000001e9b54ec8 mach_msg2_internal + 80 (mach_msg.c:201)
2   libsystem_kernel.dylib        	0x00000001e9b54de0 mach_msg_overwrite + 436 (mach_msg.c:0)
3   libsystem_kernel.dylib        	0x00000001e9b54c20 mach_msg + 24 (mach_msg.c:323)
4   CoreFoundation                	0x00000001a0eccf5c __CFRunLoopServiceMachPort + 160 (CFRunLoop.c:2624)
5   CoreFoundation                	0x00000001a0ecc600 __CFRunLoopRun + 1208 (CFRunLoop.c:3007)
6   CoreFoundation                	0x00000001a0ecbcd8 CFRunLoopRunSpecific + 608 (CFRunLoop.c:3420)
7   Foundation                    	0x000000019fdecb5c -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 212 (NSRunLoop.m:373)
8   Foundation                    	0x000000019fdec9ac -[NSRunLoop(NSRunLoop) runUntilDate:] + 64 (NSRunLoop.m:420)
9   UIKitCore                     	0x00000001a351981c -[UIEventFetcher threadMain] + 420 (UIEventFetcher.m:1207)
10  Foundation                    	0x000000019fe03428 __NSThread__start__ + 732 (NSThread.m:991)
11  libsystem_pthread.dylib       	0x00000001fd94e06c _pthread_start + 136 (pthread.c:931)
12  libsystem_pthread.dylib       	0x00000001fd9490d8 thread_start + 8 (:-1)

Thread 5:
0   libsystem_pthread.dylib       	0x00000001fd9490c4 start_wqthread + 0 (:-1)

Thread 6:
0   libsystem_pthread.dylib       	0x00000001fd9490c4 start_wqthread + 0 (:-1)

Thread 7 name:
Thread 7:
0   libsystem_kernel.dylib        	0x00000001e9b516c8 mach_msg2_trap + 8 (:-1)
1   libsystem_kernel.dylib        	0x00000001e9b54ec8 mach_msg2_internal + 80 (mach_msg.c:201)
2   libsystem_kernel.dylib        	0x00000001e9b54de0 mach_msg_overwrite + 436 (mach_msg.c:0)
3   libsystem_kernel.dylib        	0x00000001e9b54c20 mach_msg + 24 (mach_msg.c:323)
4   CoreFoundation                	0x00000001a0eccf5c __CFRunLoopServiceMachPort + 160 (CFRunLoop.c:2624)
5   CoreFoundation                	0x00000001a0ecc600 __CFRunLoopRun + 1208 (CFRunLoop.c:3007)
6   CoreFoundation                	0x00000001a0ecbcd8 CFRunLoopRunSpecific + 608 (CFRunLoop.c:3420)
7   SDL2                          	0x00000001058a9d7c audioqueue_thread + 872 (SDL_coreaudio.m:971)
8   SDL2                          	0x0000000105898440 SDL_RunThread + 48 (SDL_thread.c:333)
9   SDL2                          	0x000000010589e6dc RunThread + 12 (SDL_systhread.c:76)
10  libsystem_pthread.dylib       	0x00000001fd94e06c _pthread_start + 136 (pthread.c:931)
11  libsystem_pthread.dylib       	0x00000001fd9490d8 thread_start + 8 (:-1)


Thread 0 crashed with ARM Thread State (64-bit):
    x0: 0x0000000000000000   x1: 0x0000000000000000   x2: 0x0000000000000000   x3: 0x0000000000000000
    x4: 0x0000000000000073   x5: 0x000000000000002e   x6: 0x0000000000000001   x7: 0x000000010a520028
    x8: 0xb1f94ee6f0e49e00   x9: 0xb1f94ee4f165a0c0  x10: 0x000000000000000a  x11: 0x0000000000000000
   x12: 0x0000000000000032  x13: 0x00000000001ff800  x14: 0x00000000000007fb  x15: 0x00000000a3f820fd
   x16: 0x0000000000000148  x17: 0x0000000201813ec0  x18: 0x0000000000000000  x19: 0x0000000000000006
   x20: 0x0000000000000103  x21: 0x0000000201813fa0  x22: 0x0000000000000003  x23: 0x000000016ae8a2e0
   x24: 0x0000000000000000  x25: 0x0000000000000000  x26: 0x000000016ae8bbd9  x27: 0x0000000201813ec0
   x28: 0x0000000104fe9000   fp: 0x000000016ae89c30   lr: 0x00000001fd94fc0c
    sp: 0x000000016ae89c10   pc: 0x00000001e9b5c2ec cpsr: 0x40001000
   esr: 0x56000080  Address size fault


Binary Images:
        0x104f74000 -         0x104fe3fff Baseball '97 arm64  <284f98d1289e3e45932fa2167de3bdd2> /private/var/containers/Bundle/Application/425699E9-FAE6-41FF-A570-B2F57135925F/Baseball '97.app/Baseball '97
        0x1050c4000 -         0x1050e3fff SDL2_image arm64  <9d45720cfd1a366d94e727d2aa6eeff5> /private/var/containers/Bundle/Application/425699E9-FAE6-41FF-A570-B2F57135925F/Baseball '97.app/Frameworks/SDL2_image.framework/SDL2_image
        0x1051d4000 -         0x1051dffff libobjc-trampolines.dylib arm64e  <be553713db163c12aaa48fd6211e48ce> /private/preboot/Cryptexes/OS/usr/lib/libobjc-trampolines.dylib
        0x105708000 -         0x105813fff SDL2_ttf arm64  <1ccf5ba751c9340aa96bf9d553d22278> /private/var/containers/Bundle/Application/425699E9-FAE6-41FF-A570-B2F57135925F/Baseball '97.app/Frameworks/SDL2_ttf.framework/SDL2_ttf
        0x105880000 -         0x10596bfff SDL2 arm64  <1724009b36b83f6498bd348fa6696f8f> /private/var/containers/Bundle/Application/425699E9-FAE6-41FF-A570-B2F57135925F/Baseball '97.app/Frameworks/SDL2.framework/SDL2
        0x19fd25000 -         0x1a089afff Foundation arm64e  <d27a6ec5943c3b0e8d158840fd2914f0> /System/Library/Frameworks/Foundation.framework/Foundation
        0x1a0e79000 -         0x1a13a6fff CoreFoundation arm64e  <76a3b1983c09323e83590d4978e156f5> /System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
        0x1a30fb000 -         0x1a4c1cfff UIKitCore arm64e  <9da0d27355063712b73de0149d74c13c> /System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore
        0x1a8de6000 -         0x1a8e63ff3 libsystem_c.dylib arm64e  <7135c2c8ba5836368b46a9e6226ead45> /usr/lib/system/libsystem_c.dylib
        0x1b1096000 -         0x1b10cbfff libsystem_malloc.dylib arm64e  <73a554d086563bd0b09692186b021f4a> /usr/lib/system/libsystem_malloc.dylib
        0x1c4666000 -         0x1c46f3937 dyld arm64e  <52039c944da13638bd52020a0b5fa399> /usr/lib/dyld
        0x1e5918000 -         0x1e5920fff GraphicsServices arm64e  <3ebbd576e7d83f69bcb5b9810ddcc90e> /System/Library/PrivateFrameworks/GraphicsServices.framework/GraphicsServices
        0x1e9b50000 -         0x1e9b89fef libsystem_kernel.dylib arm64e  <21ee5290d1193c31b948431865a67738> /usr/lib/system/libsystem_kernel.dylib
        0x1fd948000 -         0x1fd954ff3 libsystem_pthread.dylib arm64e  <e4a9d6dbf93b3c88bdd185671ec22e2b> /usr/lib/system/libsystem_pthread.dylib

EOF

Here is the part of my code where the crash happens:

EWindowsEvents PM_HandleWindowsEventsLow(BOOL mainLoop)
{
	// If we're in the main loop, but the game isn't running (i.e.; the game isn't
	// active and SCUMMVAR_always_run_game isn't true), then use GetMessage() instead of
	// PeekMessage().

	SDL_Event event;
	bool atLeastOneEvent = false;

	while (SDL_PollEvent(&event)) {
		atLeastOneEvent = true;

		switch (event.type) {

     ... continues with cases

I’ll deeply apreciate any guidance :folded_hands: