How do I handle force-quit in Swift

How do I handle force quit in Swift? I received crash reports during a tesflight test. I didn't understand what it was: none of my app's symbols were present, and Xcode didn't want them either... unlike two others who were very specific.

By doing a few Google queries, I saw that [UIApplication _terminateWithStatus:] + 136 (UIApplication.m:7578). Accompanied by a SIGSEV... corresponded to a simple thing: it's a crash during a force quit.

I tested it with two iPhones, connected to my Mac, and launched the app from Xcode on each of them. I waited a bit, then quit it. It immediately went into the background and waited to launch operations with BackgroundTaskManager. I went to the app carousel and quit it with a swipe of my finger. I immediately see in the log that "sceneDidDisconnect" from SceneDelegate is called... then the immediate crash occurs, with the same elements as those received during the test flight

Incident Identifier: 97B3D25A-FCE1-44B4-B3CC-B3EDE58237D3
Distributor ID:      com.apple.TestFlight
Hardware Model:      iPhone14,4
Process:             maPollution [1021]
Path:                /private/var/containers/Bundle/Application/85ADADAF-E7AE-4F7A-ABFD-8ADE5C7BB50B/maPollution.app/maPollution
Identifier:          rva.maPollution
Version:             2.0 (9)
AppStoreTools:       16E137
AppVariant:          1:iPhone14,4:18
Beta:                YES
Code Type:           ARM-64 (Native)
Role:                Non UI
Parent Process:      launchd [1]
Coalition:           rva.maPollution [802]

Date/Time:           2025-03-29 21:52:25.0205 +0100
Launch Time:         2025-03-29 21:52:07.3201 +0100
OS Version:          iPhone OS 18.4 (22E240)
Release Type:        User
Baseband Version:    4.51.04
Report Version:      104

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x0010e19209cc9ef8 -> 0x0000001209cc9ef8 (possible pointer authentication failure)
Exception Codes: 0x0000000000000001, 0x0010e19209cc9ef8
VM Region Info: 0x1209cc9ef8 is in 0x1000000000-0x7000000000;  bytes after start: 8754339576  bytes before end: 403562520839
      REGION TYPE                 START - END      [ VSIZE] PRT/MAX SHRMOD  REGION DETAIL
      commpage (reserved)      fc0000000-1000000000 [  1.0G] ---/--- SM=NUL  reserved VM address space (unallocated)
--->  GPU Carveout (reserved) 1000000000-7000000000 [384.0G] ---/--- SM=NUL  reserved VM address space (unallocated)
      UNUSED SPACE AT END
Termination Reason: SIGNAL 11 Segmentation fault: 11
Terminating Process: exc handler [1021]

Triggered by Thread:  0


Thread 0 name:
Thread 0 Crashed:
0   libobjc.A.dylib               	0x000000019aa4b020 objc_msgSend + 32
1   UIKitCore                     	0x00000001a0f499ac -[UIApplication _terminateWithStatus:] + 136 (UIApplication.m:7578)
2   UIKitCore                     	0x00000001a00a0e2c -[_UISceneLifecycleMultiplexer _evalTransitionToSettings:fromSettings:forceExit:withTransitionStore:] + 112 (_UISceneLifecycleMultiplexer.m:819)
3   UIKitCore                     	0x00000001a0796a58 -[_UISceneLifecycleMultiplexer forceExitWithTransitionContext:scene:] + 164 (_UISceneLifecycleMultiplexer.m:482)
4   UIKitCore                     	0x00000001a0f45188 -[UIApplication workspaceShouldExit:withTransitionContext:] + 164 (UIApplication.m:4237)
5   FrontBoardServices            	0x00000001b7b19c54 __63-[FBSWorkspaceScenesClient willTerminateWithTransitionContext:]_block_invoke_2 + 72 (FBSWorkspaceScenesClient.m:635)
6   FrontBoardServices            	0x00000001b7ae9c58 -[FBSWorkspace _calloutQueue_executeCalloutFromSource:withBlock:] + 168 (FBSWorkspace.m:445)
7   FrontBoardServices            	0x00000001b7b19bf4 __63-[FBSWorkspaceScenesClient willTerminateWithTransitionContext:]_block_invoke + 128 (FBSWorkspaceScenesClient.m:632)
8   libdispatch.dylib             	0x00000001a5473584 0x1a5458000 + 112004
9   libdispatch.dylib             	0x00000001a545eab0 0x1a5458000 + 27312
10  FrontBoardServices            	0x00000001b7ae6658 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 52 (FBSSerialQueue.m:285)
11  FrontBoardServices            	0x00000001b7ae65d8 -[FBSMainRunLoopSerialQueue _targetQueue_performNextIfPossible] + 240 (FBSSerialQueue.m:309)
12  FrontBoardServices            	0x00000001b7ae64b0 -[FBSMainRunLoopSerialQueue _performNextFromRunLoopSource] + 28 (FBSSerialQueue.m:322)
13  CoreFoundation                	0x000000019d54dce4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 28 (CFRunLoop.c:1970)
14  CoreFoundation                	0x000000019d54dc78 __CFRunLoopDoSource0 + 172 (CFRunLoop.c:2014)
15  CoreFoundation                	0x000000019d54ca60 __CFRunLoopDoSources0 + 332 (CFRunLoop.c:2059)
16  CoreFoundation                	0x000000019d54bc3c __CFRunLoopRun + 840 (CFRunLoop.c:2969)
17  CoreFoundation                	0x000000019d570700 CFRunLoopRunSpecific + 572 (CFRunLoop.c:3434)
18  GraphicsServices              	0x00000001ea0b1190 0x1ea0b0000 + 4496
19  UIKitCore                     	0x00000001a018e240 -[UIApplication _run] + 816 (UIApplication.m:3845)
20  UIKitCore                     	0x00000001a018c470 UIApplicationMain + 336 (UIApplication.m:5540)
21  UIKitCore                     	0x00000001a05eaa30 UIApplicationMain(_:_:_:_:) + 104 (UIKit.swift:565)
22  maPollution                   	0x0000000102be5f14 $sSo21UIApplicationDelegateP5UIKitE4mainyyFZ11maPollution03AppB0C_Tgm5 + 28 (/:20)
23  maPollution                   	0x0000000102be5f14 static AppDelegate.$main() + 28 (AppDelegate.swift:0)
24  maPollution                   	0x0000000102be5f14 main + 116
25  dyld                          	0x00000001c3f73ad8 start + 5964 (dyldMain.cpp:1443)

Thread 1:
0   libsystem_pthread.dylib       	0x0000000227a429f0 start_wqthread + 0

Thread 2:
0   libsystem_pthread.dylib       	0x0000000227a429f0 start_wqthread + 0

Thread 3:
0   libsystem_pthread.dylib       	0x0000000227a429f0 start_wqthread + 0

Thread 4 name:
Thread 4:
0   libsystem_kernel.dylib        	0x00000001ee0d7ce4 mach_msg2_trap + 8
1   libsystem_kernel.dylib        	0x00000001ee0db39c mach_msg2_internal + 76 (mach_msg.c:201)
2   libsystem_kernel.dylib        	0x00000001ee0db2b8 mach_msg_overwrite + 428 (mach_msg.c:0)
3   libsystem_kernel.dylib        	0x00000001ee0db100 mach_msg + 24 (mach_msg.c:323)
4   CoreFoundation                	0x000000019d54c6cc __CFRunLoopServiceMachPort + 160 (CFRunLoop.c:2637)
5   CoreFoundation                	0x000000019d54bdac __CFRunLoopRun + 1208 (CFRunLoop.c:3021)
6   CoreFoundation                	0x000000019d570700 CFRunLoopRunSpecific + 572 (CFRunLoop.c:3434)
7   Foundation                    	0x000000019c1b28a8 0x19c13e000 + 477352
8   Foundation                    	0x000000019c1b16d8 0x19c13e000 + 472792
9   UIKitCore                     	0x00000001a02815ec 0x19fdc4000 + 4969964
10  Foundation                    	0x000000019c253cf8 0x19c13e000 + 1137912
11  libsystem_pthread.dylib       	0x0000000227a42afc _pthread_start + 136 (pthread.c:931)
12  libsystem_pthread.dylib       	0x0000000227a42a04 thread_start + 8

Thread 5 name:
Thread 5:
0   libsystem_kernel.dylib        	0x00000001ee0df32c __select + 8
1   CoreFoundation                	0x000000019d5a3b84 __CFSocketManager + 704 (CFSocket.c:1341)
2   libsystem_pthread.dylib       	0x0000000227a42afc _pthread_start + 136 (pthread.c:931)
3   libsystem_pthread.dylib       	0x0000000227a42a04 thread_start + 8

Thread 6:
0   libsystem_pthread.dylib       	0x0000000227a429f0 start_wqthread + 0

Thread 7:
0   libsystem_pthread.dylib       	0x0000000227a429f0 start_wqthread + 0

Thread 8:
0   libsystem_pthread.dylib       	0x0000000227a429f0 start_wqthread + 0


Thread 0 crashed with ARM Thread State (64-bit):
    x0: 0x0000000103d06960   x1: 0x00000002174a8b7c   x2: 0x0000000216c32aba   x3: 0x0000000000000000
    x4: 0x0000000000000000   x5: 0x0000000000000000   x6: 0x00000000004529c6   x7: 0xdd90e1921489bfac
    x8: 0x0000000216c32000   x9: 0x00000002168d8820  x10: 0x6ae1000103d06960  x11: 0x00ff000110fea000
   x12: 0x0000000000000030  x13: 0x0000000110fea2f0  x14: 0xdd90e19209cc9eec  x15: 0x0010e19209cc9ee8
   x16: 0x0010e19209cc9ee8  x17: 0x000000020aabcd90  x18: 0x0000000000000000  x19: 0x0000000000000000
   x20: 0x000000010333b950  x21: 0x0000000000000002  x22: 0x000000002b87008c  x23: 0x0000000000000001
   x24: 0x000000020909c000  x25: 0x0000000000000000  x26: 0x0000000000000010  x27: 0x0000000000000000
   x28: 0x0000000000002e03   fp: 0x000000016d27e050   lr: 0x00000001a0f499ac
    sp: 0x000000016d27e010   pc: 0x000000019aa4b020 cpsr: 0x20001000
   esr: 0x92000004 (Data Abort) byte read Translation fault


Binary Images:
        0x102b80000 -         0x102d3bfff maPollution arm64   /var/containers/Bundle/Application/85ADADAF-E7AE-4F7A-ABFD-8ADE5C7BB50B/maPollution.app/maPollution
        0x102f34000 -         0x102f3ffff libobjc-trampolines.dylib arm64e  <7b2292ed23143e4f929b2831fc172b43> /private/preboot/Cryptexes/OS/usr/lib/libobjc-trampolines.dylib
        0x19aa48000 -         0x19aa99beb libobjc.A.dylib arm64e  <8b2413f2456037f3b0815ccd6236f050> /usr/lib/libobjc.A.dylib
        0x19c13e000 -         0x19cdc75ff Foundation arm64e  <2bd7eb532f573300b8de478229e49115> /System/Library/Frameworks/Foundation.framework/Foundation
        0x19d4db000 -         0x19da57fff CoreFoundation arm64e  <0551baa3c6e93423b0e2be69719c1d7f> /System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
        0x19fdc4000 -         0x1a1d04a1f UIKitCore arm64e  <05197341f99832e5a847da98bb8c68bf> /System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore
        0x1a5458000 -         0x1a549daff libdispatch.dylib arm64e  <2c7db97b46f03a67af9fe26859f0008d> /usr/lib/system/libdispatch.dylib
        0x1b7aca000 -         0x1b7b9d99f FrontBoardServices arm64e  <7221a6a25de83a289a072e25e136446a> /System/Library/PrivateFrameworks/FrontBoardServices.framework/FrontBoardServices
        0x1c3f63000 -         0x1c3ffd013 dyld arm64e  <189fe4805d5b3b89928958bc88624420> /usr/lib/dyld
        0x1ea0b0000 -         0x1ea0b8c5f GraphicsServices arm64e   /System/Library/PrivateFrameworks/GraphicsServices.framework/GraphicsServices
        0x1ee0d7000 -         0x1ee110b77 libsystem_kernel.dylib arm64e  <9d196db4701331768c025b4c68701c92> /usr/lib/system/libsystem_kernel.dylib
        0x227a41000 -         0x227a4d3fb libsystem_pthread.dylib arm64e  <00306a1f11183f8690bdd18b5ed5409f> /usr/lib/system/libsystem_pthread.dylib

EOF
: SIGSEV and [UIApplication _terminateWithStatus:] and identical elements thereafter.

And this applies regardless of what I put in "SceneDidDisconnect," a print, and something to close the BGtasks if they are running (but iOS should normally kill them too, right?) .. 1 or 2 secondes after it crashes.

At the moment of the crash, the Xcode cursor is positioned on "class Appdelegate" in AppDelegate.

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

My question is: how do I handle force quit in Swift? I have another Objective-C application that does the same thing and runs identical operations in the background. If I force quit it, there is no crash.

Answered by DTS Engineer in 832415022

One quick comment on this point:

And this applies regardless of what I put in "SceneDidDisconnect," a print, and something to close the BGtasks if they are running (but iOS should normally kill them too, right?) .. 1 or 2 secondes after it crashes.

My general recommendation is that you specifically avoid doing any kind of "system" clean up on the close/exit path. That's because:

  • For things like writing data out to disk, your implicitly assuming that your app will always exit in a "clean" way. That's another way of saying "Please destroy data when my app crashes".

  • For things like memory, the system can destroy things faster than you can.

  • Many of our frameworks (case in point, BackgroundTask) heavily rely on IPC to one or more supporting daemons, which makes reliably "cleanup" impossible. In many cases your app will have already terminate before the daemon actually acts on whatever message you sent.

  • Related to that point, keep in mind that the system HAS to be able to safely clean up it's own resources regardless of what your app does.

Case in point:

...something to close the BGtasks if they are running (but iOS should normally kill them too, right?)

The system will take care of this because NOT taking care of this would mean that system resources are leaked/mishandled anytime an app crashes.

If i put "exit(0) " in SceneDiddisconnect, the crash doesnot appear , The problem is in AppDelegate. But what problem ? I have put this code : this is not called.

Looking at our code, this line:

1   UIKitCore                     	0x00000001a0f499ac -[UIApplication _terminateWithStatus:] + 136 (UIApplication.m:7578)

Is the point where UIApplication specifically calls "applicationWillTerminate" in your application delegate. So, my guess would be that your AppDelegate object is actually being deallocated, probably by something your SceneDelegate is doing.

__
Kevin Elliott
DTS Engineer, CoreOS/Hardware

Accepted Answer

If i put "exit(0) " in SceneDiddisconnect, the crash doesnot appear , The problem is in AppDelegate. But what problem ? I have put this code : this is not called.

func applicationWillTerminate(_ application: UIApplication){
print("I will terminate")
}

One quick comment on this point:

And this applies regardless of what I put in "SceneDidDisconnect," a print, and something to close the BGtasks if they are running (but iOS should normally kill them too, right?) .. 1 or 2 secondes after it crashes.

My general recommendation is that you specifically avoid doing any kind of "system" clean up on the close/exit path. That's because:

  • For things like writing data out to disk, your implicitly assuming that your app will always exit in a "clean" way. That's another way of saying "Please destroy data when my app crashes".

  • For things like memory, the system can destroy things faster than you can.

  • Many of our frameworks (case in point, BackgroundTask) heavily rely on IPC to one or more supporting daemons, which makes reliably "cleanup" impossible. In many cases your app will have already terminate before the daemon actually acts on whatever message you sent.

  • Related to that point, keep in mind that the system HAS to be able to safely clean up it's own resources regardless of what your app does.

Case in point:

...something to close the BGtasks if they are running (but iOS should normally kill them too, right?)

The system will take care of this because NOT taking care of this would mean that system resources are leaked/mishandled anytime an app crashes.

If i put "exit(0) " in SceneDiddisconnect, the crash doesnot appear , The problem is in AppDelegate. But what problem ? I have put this code : this is not called.

Looking at our code, this line:

1   UIKitCore                     	0x00000001a0f499ac -[UIApplication _terminateWithStatus:] + 136 (UIApplication.m:7578)

Is the point where UIApplication specifically calls "applicationWillTerminate" in your application delegate. So, my guess would be that your AppDelegate object is actually being deallocated, probably by something your SceneDelegate is doing.

__
Kevin Elliott
DTS Engineer, CoreOS/Hardware

You gave me a direction to search : SceneDelegate. In "WillConnect to" , There was a line :

UIApplication.shared.delegate = self

i removed this line, rebuild with no error, what was this line doing here ? I do not remember.. And test this version. Put it in back ground , wait , force-kill ... "sceneDidDisconnect" is executed AND applicationWillTerminate in appDelegate too.

And no more crash. Thank you.

And no more crash. Thank you.

You're very welcome.

__
Kevin Elliott
DTS Engineer, CoreOS/Hardware

How do I handle force-quit in Swift
 
 
Q