Objective
When we run 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 the hell is this library for? 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.
SolveThe solution is simple, when assigning a value, type conversion:
Mutablearr: Variable array arr: Immutable group //mutabledic: Variable dictionary dic: immutable dictionary //assignment requires a mutable type conversion Mutablearr = [Nsmutablearray Arraywitharray:arr]; Mutabledic = [Nsmutabledictionary dictionarywithdictionary:dic];
Libc++abi.dylib:terminate_handler unexpectedly threw an exception error summary