Usually when a crash is happening on iOS when the simulator or device is attached to Xcode I get a crash reason like the following example:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSJSONSerialization dataWithJSONObject:options:error:]: value parameter is nil'
When I look at the crashes of my published app in the Xcode Organizer using "Open in Project..." I see on which line of code the crash did happen. But is it also possible to get a reason for the crash like the above?
I'm asking this about Xcode Version 13.0.
The crash report is attached.
Oh, that’s very enlightening. Consider the backtrace of the crashing thread:
0 CoreFoundation … __exceptionPreprocess + 220 …
1 libobjc.A.dylib … objc_exception_throw + 60 …
2 Foundation … _writeJSONNumber + 1244 …
3 Foundation … ___writeJSONObject_block_invoke + 388 …
4 CoreFoundation … __NSDICTIONARY_IS_CALLING_OUT_TO_A_BLOCK__ + 24 …
5 CoreFoundation … -[__NSDictionaryI enumerateKeysAndObjectsWithOptions:usingBlock:] + 184 …
6 Foundation … _writeJSONObject + 508 …
7 Foundation … ___writeJSONArray_block_invoke + 260 …
8 CoreFoundation … __NSARRAY_IS_CALLING_OUT_TO_A_BLOCK__ + 24 …
9 CoreFoundation … -[__NSArrayM enumerateObjectsWithOptions:usingBlock:] + 196 …
10 Foundation … _writeJSONArray + 340 …
11 Foundation … ___writeJSONObject_block_invoke + 388 …
12 CoreFoundation … __NSDICTIONARY_IS_CALLING_OUT_TO_A_BLOCK__ + 24 …
13 CoreFoundation … -[__NSDictionaryM enumerateKeysAndObjectsWithOptions:usingBlock:] + 208 …
14 Foundation … _writeJSONObject + 508 …
15 Foundation … -[_NSJSONWriter dataWithRootObject:options:] + 88 …
16 Foundation … +[NSJSONSerialization dataWithJSONObject:options:error:] + 120 …
17 Rrrr … -[LocationManager sendPendingLocationObjects:] + 1988
The exception isn’t coming directly from +dataWithJSONObject:options:error:
but from something much deeper, namely _writeJSONNumber
. Also, the pattern in the backtrace gives you a rough idea of the nesting, namely dictionary > array > dictionary > number. Finally, disassembling _writeJSONNumber
(see below) reveals three potential causes:
Invalid number value (NaN) in JSON write
Invalid number value (infinite) in JSON write
Invalid number type in JSON write (%c)
I suspect it’s one of the first two.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
(lldb) disas -n _writeJSONNumber
Foundation`_writeJSONNumber:
0x1826fa5bc <+0>: pacibsp
…
0x1826fa9e0 <+1060>: brk #0x1
0x1826fa9e4 <+1064>: adrp x8, 348530
0x1826fa9e8 <+1068>: ldr x0, [x8, #0x670]
0x1826fa9ec <+1072>: adrp x8, 358092
0x1826fa9f0 <+1076>: ldr x8, [x8, #0xe70]
0x1826fa9f4 <+1080>: ldr x2, [x8]
0x1826fa9f8 <+1084>: adrp x8, 297027
0x1826fa9fc <+1088>: add x1, x8, #0x8d1 ; =0x8d1
0x1826faa00 <+1092>: adrp x3, 364346
0x1826faa04 <+1096>: add x3, x3, #0x768 ; =0x768
(lldb) po 0x1826fa000+(364346<<12)+0x768
Invalid number value (NaN) in JSON write
0x1826faa08 <+1100>: b 0x1826faa8c ; <+1232>
0x1826faa0c <+1104>: adrp x8, 348530
0x1826faa10 <+1108>: ldr x0, [x8, #0x670]
0x1826faa14 <+1112>: adrp x8, 358092
0x1826faa18 <+1116>: ldr x8, [x8, #0xe70]
0x1826faa1c <+1120>: ldr x2, [x8]
0x1826faa20 <+1124>: adrp x8, 297027
0x1826faa24 <+1128>: add x1, x8, #0x8d1 ; =0x8d1
0x1826faa28 <+1132>: adrp x3, 364346
0x1826faa2c <+1136>: add x3, x3, #0x788 ; =0x788
(lldb) po 0x1826fa000+(364346<<12)+0x788
Invalid number value (infinite) in JSON write
0x1826faa30 <+1140>: b 0x1826faa8c ; <+1232>
0x1826faa34 <+1144>: adrp x8, 348530
0x1826faa38 <+1148>: ldr x19, [x8, #0x620]
0x1826faa3c <+1152>: mov x0, x20
0x1826faa40 <+1156>: mov x1, x22
0x1826faa44 <+1160>: bl 0x180bc7e58 QQQ objc_msgSend
0x1826faa48 <+1164>: ldrsb x8, [x0]
0x1826faa4c <+1168>: adrp x9, 348522
0x1826faa50 <+1172>: ldr x1, [x9, #0x980]
(lldb) p *(char **)(0x1826fa000+(348522<<12)+0x980)
(char *) $5 = 0x00000001caf3a2e2 "stringWithFormat:"
0x1826faa54 <+1176>: str x8, [sp, #-0x10]!
0x1826faa58 <+1180>: adrp x2, 364346
0x1826faa5c <+1184>: add x2, x2, #0x7a8 ; =0x7a8
(lldb) po 0x1826fa000+(364346<<12)+0x7a8
Invalid number type in JSON write (%c)
0x1826faa60 <+1188>: mov x0, x19
0x1826faa64 <+1192>: bl 0x180bc7e58 QQQ objc_msgSend
0x1826faa68 <+1196>: add sp, sp, #0x10 ; =0x10
0x1826faa6c <+1200>: mov x3, x0
0x1826faa70 <+1204>: adrp x8, 348530
0x1826faa74 <+1208>: ldr x0, [x8, #0x670]
0x1826faa78 <+1212>: adrp x8, 358092
0x1826faa7c <+1216>: ldr x8, [x8, #0xe70]
0x1826faa80 <+1220>: ldr x2, [x8]
0x1826faa84 <+1224>: adrp x8, 297027
0x1826faa88 <+1228>: add x1, x8, #0x8d1 ; =0x8d1
(lldb) p (char *)(0x1826fa000+(297027<<12)+0x8d1)
(char *) $3 = 0x00000001caf3d8d1 "exceptionWithName:reason:userInfo:"
0x1826faa8c <+1232>: mov x4, #0x0
0x1826faa90 <+1236>: bl 0x180bc7e58
(lldb) disas -c 3 -s 0x180bc7e58
0x180bc7e58: adrp x16, 100442
0x180bc7e5c: add x16, x16, #0x5c0 ; =0x5c0
0x180bc7e60: br x16
(lldb) p/a 0x180bc7000+(100442<<12)+0x5c0
(long) $0 = 0x00000001994215c0 libobjc.A.dylib`objc_msgSend
0x1826faa94 <+1240>: bl 0x181031af4 ; symbol stub for: objc_exception_throw