简体   繁体   中英

What part of the App Delegate life-cycle is called when an app in the foreground has an out of memory crash?

Trying to debug a crash that a user is having that isn't showing up in our crash reporting tool or in our log files. Have a theory it might be down to memory pressure, but not sure if applicationWillTerminate will be called if iOS kills an app application in the foreground. We write to our log file in applicationWillTerminate but it apparently isn't being called during this crash.

If the app crashes, no lifecycle method is called reliably. Instead you can create & register a global exception handler which gets invoked in this case:

func exceptionHandler(exception: NSException) {
  print("*** UNHANDLED EXCEPTION ***")
  print(exception)
  print("CALL STACK:")
  print(exception.callStackSymbols.joined(separator: "\n"))
}

Register this function using NSSetUncaughtExceptionHandler , eg in your UIApplicationDelegate.application:didFinishLaunchingWithOptions: :

NSSetUncaughtExceptionHandler(exceptionHandler)

System had to trigger applicationWillTerminate if it up to kill the app. However you can't be sure how much time you have to before app actually will be killed. Maybe it just not enough time to write the log. Before killing app due to memory consumption system should send memory warning. You can test last one by simulating memory warning on Simulator. If applicationWillTerminate didn't called then want terminated by system and you crash not related directly to memory consumption.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM