Objective
When we run the Xcode project, we sometimes encounter "Libc++abi.dylib:terminate_handler unexpectedly threw an exception" error, The app inexplicably crash off, enter the BT command in the console, view the call stack, and the result is confused:
Analysis
Now the only point of information is Libc++abi.dylib, What exactly does this library do? From the suffix view, is a dynamic library, then will it be because of some dynamic errors? By experience, the general dynamic error is basically due to the dynamic type error, in the Object-c language, the possibility of a dynamic type error can exist in the transformation between the immutable type and the mutable type, then our error-checking scope will be limited to the immutable type and the variable type conversion, Are we modifying a immutable type? Of course, the compiler is not so silly, if directly to an immutable type modification operation, will be directly error, then there is another possibility, the program assigns an immutable type to a mutable type, and then modifies the mutable type, which can be checked by static, but dynamically run, A type error occurs. Based on the above analysis, we can track breakpoints, we will find that the program in the Mutable object to add, set and other operations, and this object is actually assigned to an immutable object. A common case is to assign a Nsarray object to a Nsmutablearray object, then delete, add, and so on, or assign a Nsdictionary object to a Nsmutabledictionary object. Then the set and other operations are performed.
The workaround is simple, and the type conversion is done when assigning values:
- Mutablearr: Variable array arr: non-variable group
- Mutabledic: mutable dictionary dic: immutable Dictionary
- Variable type conversions are required when assigning a value
- Mutablearr = [Nsmutablearray Arraywitharray:arr];
- Mutabledic = [Nsmutabledictionary dictionarywithdictionary:dic];
- The array may be empty,
Libc++abi.dylib:terminate_handler unexpectedly threw an exception error summary