Common iOS crash reasons
Crashes cause an unpleasent experience to the users and are bad for the app. I have listed down a few of the common reasons that lead to ios apps crashing and few ways to help identify and fix the issues
Memory Access Crashes:
These crashes occur when the app doesn’t access memory properly for eg accessing an undefined memory address or using a variable on multiple threads at once. These crashes can be identified by the exception type “EXC_BAD_ACCESS”. For fixing these kinds of crashes the backtrace of the crash can give a good idea about where the invalid memory access occurred. Additionally apple provides a few tools that can help diagnose these crashes:
Address Sanitizer: Can help catch memory corruption issues like buffer overflow and dangling pointers.
Undefined Behaviour Sanitizer: Can help catch undefined behaviours like integer overflows, accessing null pointers, out of bounds access.
Thread Sanitizer: Can help detect data races caused by multiple threads using a variable at the same time. These kinds of crashes can be fixed by using atomic variables and synchronising access to variables using @synchronized and NSLock’s.
For more info check out: https://developer.apple.com/documentation/xcode/investigating-memory-access-crashes
Zombie Objects
These crashes occur when the app tries to access a deallocated memory. These crashes can be identified by their stack trace, they would mainly contain objc_msgSend, objc_retain, objc_release as objective c is can’t send a message to/retain/release a deallocated object. For diagnosing these kinds of crashes Xcode has a zombie instrument that can be used.
To avoid these crashes take special care about the lifetime of objects that can have weak or unowned references since those are typically the source for these kinds of crashes.
For more info check out: https://developer.apple.com/documentation/xcode/investigating-crashes-for-zombie-objects
Watchdog Terminations
These crashes occur when the app is unresponsive for a significant amount of time. This means that the main thread is blocked and cannot respond to user touches/events. This can happen due to a number of reasons such as synchronous networking, loading and processing large amounts of data such as large json files, updating/creating a large core data database, etc. These crashes would typically contain the word “Watchdog” in the stack trace. To fix these kinds of crashes all code that is non essential to ui updates should be moved to the background thread, this would make the main thread available for processing ui events making your app more responsive.
For more info check out: https://developer.apple.com/documentation/xcode/addressing-watchdog-terminations
Using ui code on background thread
These crashes occur when the app tries to use ui specific code on a background thread. These are straightforward to identify and fix through the crash backtrace. To prevent these situations Xcode provides a Main thread checker that would notify you when these kinds of situations occur.
App using too much memory
In cases of memory pressure iOS asks the open apps to release memory with a low memory notification, however if that is not enough the os may terminate apps. To prevent these kinds of crashes firstly the app should free all unnecessary memory when it receives a low memory notification. Additionally try not to load large objects into memory, eliminate any memory leaks and clear references to unused objects. Additionally if your app uses core data try saving the context before it accumulates too many changes as the unsaved changes reside in memory. Following is a good resource on gathering more info about the apps memory: https://developer.apple.com/documentation/xcode/identifying-high-memory-use-with-jetsam-event-reports
For more info check out: https://developer.apple.com/documentation/xcode/identifying-high-memory-use-with-jetsam-event-reports
Hope this helps, thanks for reading!