Search code examples
iosobjective-cdebuggingunrecognized-selectornsnull

How to know where is my app crashing, only with this log info?


I need to detect where is my app crashing, I tried debugging line by line, but suddenly, the debugger jumps from the "assembler code" (is that the proper name?) into the crash, so I can't tell you exactly what the offending code is. I am doing text to Speech and logging my app into indigo when the crash happens. How can I make sense of this log info?:

TIMESTAMP Indigo[1675:621373] -[NSNull length]: unrecognized selector sent to instance 0x1a7c38ef8
TIMESTAMP Indigo[1675:621373] invalid mode 'kCFRunLoopCommonModes' provided to CFRunLoopRunSpecific - break on _CFRunLoopError_RunCalledWithInvalidMode to debug. This message will only appear once per execution.
TIMESTAMP Indigo[1675:621373] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance 0x1a7c38ef8'
*** First throw call stack:
(0x181aeefd8 0x180550538 0x181af5ef4 0x181af2f4c 0x1819eed2c 0x181ad4ff4 0x181ad513c 0x187c28234 0x100162f8c 0x1001a2bd8 0x187f63114 0x187f6332c 0x187e1c6e0 0x187e18978 0x187e1834c 0x187dfcfbc 0x1001a55b0 0x100041da8 0x100041b8c 0x1000e7c4c 0x1820f81fc 0x18210fef8 0x1825bd804 0x182502760 0x1824f2b18 0x1825bfba0 0x1009bda10 0x1009c2b78 0x181a9d0c0 0x181a9acdc 0x1819cad94 0x183434074 0x187c83130 0x10002160c 0x1809d959c)
libc++abi.dylib: terminating with uncaught exception of type NSException

Solution

  • You need to set a breakpoint in objc_exception_throw. Xcode has some UI niceties for setting that up, but the easiest way to explain to do it without digging up screenshots, is to enter the command in your debugger console (before the crash has occurred):

    b objc_exception_throw
    

    and then when the unrecognized selector exception is thrown the debugger will stop there instead of waiting for the exception to cause the crash-proper, where the stack is less useful.

    The exception itself is also revealing. You're sending the message length to an NSNull. I bet you've got a collection of objects that you think will be all NSStrings somewhere, but you've got occasional NSNulls appearing in that collection. I've seen that happen in iOS client code consuming JSON results from various web-services---when the backend doesn't provide all the information the client is expecting for example.

    UPDATE: Set b objc_exception_throw grafically in XCode clicking: Debug > Breakpoints > Create Exception Breakpoint