iOS 17: objc_msgSend + 32 EXC_BAD_ACCESS KERN_INVALID_ADDRESS Crash

Since iOS 17 was released, we've observed a surge in crashes: 65,000 crashes for 1,000 users who have already updated their devices to iOS 17.

Here is the stack trace:

0  libobjc.A.dylib                0x4820 objc_msgSend + 32
1  CoreFoundation                 0x2fc78 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 148
2  CoreFoundation                 0x2f640 ___CFXRegistrationPost_block_invoke + 88
3  CoreFoundation                 0x2f588 _CFXRegistrationPost + 440
4  CoreFoundation                 0x2ead8 _CFXNotificationPost + 724
5  Foundation                     0x2b7e4 -[NSNotificationCenter postNotificationName:object:userInfo:] + 92
6  UIKitCore                      0x18f464 -[UIApplication _stopDeactivatingForReason:] + 1240
7  UIKitCore                      0x13ae0c -[_UISceneLifecycleMultiplexer _performBlock:withApplicationOfDeactivationReasons:fromReasons:] + 280
8  UIKitCore                      0x139848 -[_UISceneLifecycleMultiplexer _evalTransitionToSettings:fromSettings:forceExit:withTransitionStore:] + 608
9  UIKitCore                      0x1391b0 -[_UISceneLifecycleMultiplexer uiScene:transitionedFromState:withTransitionContext:] + 248
10 UIKitCore                      0x139080 __186-[_UIWindowSceneFBSSceneTransitionContextDrivenLifecycleSettingsDiffAction _performActionsForUIScene:withUpdatedFBSScene:settingsDiff:fromSettings:transitionContext:lifecycleActionType:]_block_invoke + 148
11 UIKitCore                      0x138f88 +[BSAnimationSettings(UIKit) tryAnimatingWithSettings:fromCurrentState:actions:completion:] + 736
12 UIKitCore                      0x138810 _UISceneSettingsDiffActionPerformChangesWithTransitionContextAndCompletion + 224
13 UIKitCore                      0x1384c0 -[_UIWindowSceneFBSSceneTransitionContextDrivenLifecycleSettingsDiffAction _performActionsForUIScene:withUpdatedFBSScene:settingsDiff:fromSettings:transitionContext:lifecycleActionType:] + 316
14 UIKitCore                      0x4b78d0 __64-[UIScene scene:didUpdateWithDiff:transitionContext:completion:]_block_invoke.225 + 612
15 UIKitCore                      0x137644 -[UIScene _emitSceneSettingsUpdateResponseForCompletion:afterSceneUpdateWork:] + 216
16 UIKitCore                      0x1374b4 -[UIScene scene:didUpdateWithDiff:transitionContext:completion:] + 244
17 UIKitCore                      0x1372f4 -[UIApplicationSceneClientAgent scene:handleEvent:withCompletion:] + 336
18 FrontBoardServices             0xd99c -[FBSScene updater:didUpdateSettings:withDiff:transitionContext:completion:] + 624
19 FrontBoardServices             0xd70c __94-[FBSWorkspaceScenesClient _queue_updateScene:withSettings:diff:transitionContext:completion:]_block_invoke_2 + 152
20 FrontBoardServices             0xd5a8 -[FBSWorkspace _calloutQueue_executeCalloutFromSource:withBlock:] + 168
21 FrontBoardServices             0xd4c4 __94-[FBSWorkspaceScenesClient _queue_updateScene:withSettings:diff:transitionContext:completion:]_block_invoke + 344
22 libdispatch.dylib              0x4300 _dispatch_client_callout + 20
23 libdispatch.dylib              0x7d48 _dispatch_block_invoke_direct + 284
24 FrontBoardServices             0x99ec __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 52
25 FrontBoardServices             0x996c -[FBSMainRunLoopSerialQueue _targetQueue_performNextIfPossible] + 240
26 FrontBoardServices             0x9844 -[FBSMainRunLoopSerialQueue _performNextFromRunLoopSource] + 28
27 CoreFoundation                 0x37acc __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 28
28 CoreFoundation                 0x36d48 __CFRunLoopDoSource0 + 176
29 CoreFoundation                 0x354fc __CFRunLoopDoSources0 + 244
30 CoreFoundation                 0x34238 __CFRunLoopRun + 828
31 CoreFoundation                 0x33e18 CFRunLoopRunSpecific + 608
32 GraphicsServices               0x35ec GSEventRunModal + 164
33 UIKitCore                      0x22f350 -[UIApplication _run] + 888
34 UIKitCore                      0x22e98c UIApplicationMain + 340
35 Elli                           0x671304 main + 18 (AppDelegate.swift:18)
36 ???                            0x1c1427d44 (Missing)

I have managed to reliably replicate a crash using the following steps: minimizing the application, opening any other application, and then returning to our application, which almost consistently leads to a crash.

But Xcode just show it, without any additional information:

I appreciate any assistance in resolving these issues.

I found the issue: we added observers for NotificationService, but did not remove it. So everything is fixed with:

NotificationCenter.default.removeObserver(self)

Crashed: NSManagedObjectContext 0x2837bb400 0 libobjc.A.dylib 0x4820 objc_msgSend + 32 1 CoreData 0x5837c -[NSManagedObjectContext dispose:] + 400 2 CoreData 0x58098 -[NSManagedObjectContext dealloc] + 604 3 CoreData 0x57d7c __internalBlockToDeallocNSManagedObjectContext_block_invoke + 56 4 CoreData 0x2074c developerSubmittedBlockToNSManagedObjectContextPerform + 156 5 libdispatch.dylib 0x4300 _dispatch_client_callout + 20 6 libdispatch.dylib 0xb894 _dispatch_lane_serial_drain + 748 7 libdispatch.dylib 0xc3c4 _dispatch_lane_invoke + 380 8 libdispatch.dylib 0x17004 _dispatch_root_queue_drain_deferred_wlh + 288 9 libdispatch.dylib 0x16878 _dispatch_workloop_worker_thread + 404 10 libsystem_pthread.dylib 0x1964 _pthread_wqthread + 288 11 libsystem_pthread.dylib 0x1a04 start_wqthread + 8

in iOS17 crash

iOS 17: objc_msgSend + 32 EXC_BAD_ACCESS KERN_INVALID_ADDRESS Crash
 
 
Q