SwiftUI app crashes randomly on iOS 17

Some users report random crashing when just navigating the app. The app is really simple, here is the structure (it's MVP so most of the screens are not done yet):


    @State private var isChatModalPresented = false
    @State private var selectedTab = 0
    @State private var previousTab = 0

    var body: some View {
        TabView(selection: $selectedTab) {
            Text("Dashboard")
                .tabItem {
                    Label("Dashboard", systemImage: "house")
                }
                .tag(0)
            Text("Training")
                .tabItem {
                    Label("Training", systemImage: "flame")
                }
                .tag(1)
            Text("")
                .tabItem {
                    Label("Chat", systemImage: "bubble.left")
                }
                .tag(2)
            Text("Recovery")
                .tabItem {
                    Label("Recovery", systemImage: "heart")
                }
                .tag(3)
            Text("Community")
                .tabItem {
                    Label("Community", systemImage: "person.3")
                }
                .tag(4)
        }
        .onChange(of: selectedTab) { neco in
            if selectedTab == 2 {
                self.isChatModalPresented = true
                selectedTab = previousTab
            } else {
                previousTab = selectedTab
            }
        }
        .fullScreenCover(isPresented: $isChatModalPresented) {
            ChatView(isPresented: $isChatModalPresented)
        }
    }

}

It's supposed to be a standard tabview, except that when you tap on the middle item, it's supposed to present a view from the bottom using fullScreenCover. Is this code correct?

I am supplying the crash log:

Exception Subtype: KERN_PROTECTION_FAILURE at 0x0f00002a00000000 -> 0x0000002a00000000 (possible pointer authentication failure)
Exception Codes: 0x0000000000000002, 0x0f00002a00000000
VM Region Info: 0x2a00000000 is in 0x1000000000-0x7000000000;  bytes after start: 111669149696  bytes before end: 300647710719
      REGION TYPE                 START - END      [ VSIZE] PRT/MAX SHRMOD  REGION DETAIL
      commpage (reserved)      fc0000000-1000000000 [  1.0G] ---/--- SM=NUL  ...(unallocated)
--->  GPU Carveout (reserved) 1000000000-7000000000 [384.0G] ---/--- SM=NUL  ...(unallocated)
      UNUSED SPACE AT END
Termination Reason: SIGNAL 10 Bus error: 10
Terminating Process: exc handler [25839]

Triggered by Thread:  0


Thread 0 name:
Thread 0 Crashed:
0   libobjc.A.dylib               	0x0000000184eb98a4 lookUpImpOrForward + 72 (objc-runtime-new.mm:7331)
1   libobjc.A.dylib               	0x0000000184eb4cc4 _objc_msgSend_uncached + 68
2   UIKitCore                     	0x000000018f01f46c -[UIViewController dealloc] + 860 (UIViewController.m:3270)
3   UIKitCore                     	0x000000018f0e8a88 -[UINavigationController dealloc] + 296 (UINavigationController.m:871)
4   UIKitCore                     	0x000000018f4ce920 -[_UISplitViewControllerColumnContents .cxx_destruct] + 44 (UISplitViewControllerPanelImpl.m:438)
5   libobjc.A.dylib               	0x0000000184eb5374 object_cxxDestructFromClass(objc_object*, objc_class*) + 116 (objc-class.mm:457)
6   libobjc.A.dylib               	0x0000000184eb509c objc_destructInstance + 80 (objc-runtime-new.mm:9057)
7   libobjc.A.dylib               	0x0000000184eb503c _objc_rootDealloc + 80 (NSObject.mm:2153)
8   CoreFoundation                	0x000000018cb51f48 cow_cleanup + 164 (NSDictionaryM.m:141)
9   CoreFoundation                	0x000000018cb51e54 -[__NSDictionaryM dealloc] + 148 (NSDictionaryM.m:407)
...
36  UIKitCore                     	0x000000018ef9eedc UIApplicationMain + 340 (UIApplication.m:5270)
37  SwiftUI                       	0x00000001919b0898 closure #1 in KitRendererCommon(_:) + 176 (UIKitApp.swift:51)
38  SwiftUI                       	0x00000001919b06dc runApp<A>(_:) + 152 (UIKitApp.swift:14)
39  SwiftUI                       	0x000000019162125c static App.main() + 128 (App.swift:114)
40  REDACTED                     	0x00000001044243f4 static REDACTED.$main() + 52 (REDACTER.swift:0)
41  REDACTED                     	0x00000001044243f4 main + 64
42  dyld                          	0x00000001af8e2dcc start + 2240 (dyldMain.cpp:1269)


It seems that something is happening under the hood regarding deallocation inside underlying UIKit views so I suspect this main navigation is the culprit?

Replies

I am not sure to understand the logic in onChange.

Are you sure you want to test selectedTab

        .onChange(of: selectedTab) { neco in
            if selectedTab == 2 {

and not neco

        .onChange(of: selectedTab) { neco in
            if neco == 2 {
                self.isChatModalPresented = true
                selectedTab = previousTab
            } else {
                previousTab = selectedTab
            }
        }

When do you reset self.isChatModalPresented to false ?