当时iPhone14系列手机升级到iOS16.5.1系统以后,当用户登录后再次退出登录闪退货登录后退出登录闪退。
由于SwiftUI提倡用struct代替类,导致悲剧产生,闪退时无法打印是那个结构体(class类实现时会打印类名),因为是struct也没有deinit跟踪内存是否泄漏。开启僵尸进程打印只有一个这样的鬼话:
2023-06-30 19:52:42.637552+0800 xingqun_ios[4191:551916] *** -[_TtGC7SwiftUI41StyleContextSplitViewNavigationControllerVS_19SidebarStyleContext_ removeChildViewController:]: message sent to deallocated instance 0x10d02d000
定位闪退在main函数哪里。
iPhone14以下手机正常,只有iPhone14系列手机系统是iOS16才会出现。
经过三天个反复定位和查资料发现是iOS的导航栏变更产生的。iOS16要用NavigationStack代替NavigationView。
var body: some View { if #available(iOS 16.0, *) { NavigationStack { VStack(spacing: 0) { LoginTitleView(dismiss: .init(dismissAction: dismiss, type: .normal), from: .loginBySms) { toPage = .toLoginPassword } phoneInput // 新用户进入验证码注册页面 老用户进入验证码登录页面 switch loginModel.isNew { case true: register case false: loginByCode default: Spacer() } } .navigation(item: $toPage, destination: destination) .animation(.default, value: loginModel.isNew) .fullBackground(.white) .ignoresSafeArea(.keyboard, edges: .bottom) } .dismissKeyboard([.tap, .simulDrag]) .noPaddingFullBackground(Color.white) } else { NavigationView { VStack(spacing: 0) { LoginTitleView(dismiss: .init(dismissAction: dismiss, type: .normal), from: .loginBySms) { toPage = .toLoginPassword } phoneInput // 新用户进入验证码注册页面 老用户进入验证码登录页面 switch loginModel.isNew { case true: register case false: loginByCode default: Spacer() } } .navigation(item: $toPage, destination: destination) .animation(.default, value: loginModel.isNew) .fullBackground(.white) .ignoresSafeArea(.keyboard, edges: .bottom) } .dismissKeyboard([.tap, .simulDrag]) .noPaddingFullBackground(Color.white) } }
闪退日志:
Exception Type: EXC_BAD_ACCESS (SIGBUS) Exception Subtype: KERN_PROTECTION_FAILURE at 0x0000001200000000 Exception Codes: 0x0000000000000002, 0x0000001200000000 VM Region Info: 0x1200000000 is in 0x1000000000-0x7000000000; bytes after start: 8589934592 bytes before end: 403726925823 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 [638] Triggered by Thread: 0 Thread 0 name: Dispatch queue: com.apple.main-thread Thread 0 Crashed: 0 libobjc.A.dylib 0x1964fc414 lookUpImpOrForward + 64 1 libobjc.A.dylib 0x1964f6cc4 _objc_msgSend_uncached + 68 2 UIKitCore 0x19f6ca538 -[UIViewController dealloc] + 772 3 UIKitCore 0x19f6c8160 -[UINavigationController dealloc] + 308 4 UIKitCore 0x19f95112c -[_UISplitViewControllerColumnContents .cxx_destruct] + 44 5 libobjc.A.dylib 0x1964f60a4 object_cxxDestructFromClass(objc_object*, objc_class*) + 116 6 libobjc.A.dylib 0x1964fae00 objc_destructInstance + 80 7 libobjc.A.dylib 0x1965044fc _objc_rootDealloc + 80 8 CoreFoundation 0x19d48518c cow_cleanup + 168 9 CoreFoundation 0x19d4bd458 -[__NSDictionaryM dealloc] + 148 10 libobjc.A.dylib 0x1964f60a4 object_cxxDestructFromClass(objc_object*, objc_class*) + 116 11 libobjc.A.dylib 0x1964fae00 objc_destructInstance + 80 12 libobjc.A.dylib 0x1965044fc _objc_rootDealloc + 80 13 libobjc.A.dylib 0x1964f60a4 object_cxxDestructFromClass(objc_object*, objc_class*) + 116 14 libobjc.A.dylib 0x1964fae00 objc_destructInstance + 80 15 libobjc.A.dylib 0x1965044fc _objc_rootDealloc + 80 16 UIKitCore 0x19f6ca96c -[UIResponder dealloc] + 124 17 UIKit 0x2277b3f10 -[UIResponderAccessibility dealloc] + 56 18 UIKitCore 0x19f6ca670 -[UIViewController dealloc] + 1084 19 libobjc.A.dylib 0x1964f61d4 AutoreleasePoolPage::releaseUntil(objc_object**) + 196 20 libobjc.A.dylib 0x1964f9bdc objc_autoreleasePoolPop + 256 21 UIKitCore 0x19f66a44c -[_UIAfterCACommitBlock run] + 92 22 UIKitCore 0x19f66a36c -[_UIAfterCACommitQueue flush] + 168 23 UIKitCore 0x19f66a280 _runAfterCACommitDeferredBlocks + 496 24 UIKitCore 0x19f508be8 _cleanUpAfterCAFlushAndRunDeferredBlocks + 108 25 UIKitCore 0x19f9c7c18 _UIApplicationFlushCATransaction + 72 26 UIKitCore 0x19fb184b0 _UIUpdateSequenceRun + 84 27 UIKitCore 0x1a017cc8c schedulerStepScheduledMainSection + 144 28 UIKitCore 0x1a017c1e8 runloopSourceCallback + 92 29 CoreFoundation 0x19d52e128 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 28 30 CoreFoundation 0x19d53a7b4 __CFRunLoopDoSource0 + 176 31 CoreFoundation 0x19d4bf5e8 __CFRunLoopDoSources0 + 244 32 CoreFoundation 0x19d4d50d4 __CFRunLoopRun + 828 33 CoreFoundation 0x19d4da3ec CFRunLoopRunSpecific + 612 34 GraphicsServices 0x1d897735c GSEventRunModal + 164 35 UIKitCore 0x19f8676e8 -[UIApplication _run] + 888 36 UIKitCore 0x19f86734c UIApplicationMain + 340