9

我收到以下索引超出范围错误:

*** Terminating app due to uncaught exception 'NSRangeException', reason: 
'*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
*** First throw call stack:
(0x2263052 0x24c7d0a 0x224fdb8 0x2f4a7 0x2264ec9 0x81e299 0x81e306 0x75aa30 
0x75ac56 0x741384 0x734aa9 0x39a9fa9 0x22371c5 0x219c022 0x219a90a 0x2199db4 
0x2199ccb 0x39a8879 0x39a893e 0x732a9b 0x1e5b 0x1dc5 0x1)

我确切地知道错误的含义,但我发现这些错误很难修复,因为由于某种原因,调用堆栈没有告诉我调用数组的代码行。这是来自线程 1 的调用堆栈:

#0  0x9706d9c6 in __pthread_kill ()
#1  0x93e2cf78 in pthread_kill ()
#2  0x93e1dbdd in abort ()
#3  0x02859e78 in dyld_stub__Unwind_DeleteException ()
#4  0x0285789e in default_terminate() ()
#5  0x024c7f4b in _objc_terminate ()
#6  0x028578de in safe_handler_caller(void (*)()) ()
#7  0x02857946 in __cxa_bad_typeid ()
#8  0x02858b3e in __cxa_current_exception_type ()
#9  0x024c7e49 in objc_exception_rethrow ()
#10 0x02199e10 in CFRunLoopRunSpecific ()
#11 0x02199ccb in CFRunLoopRunInMode ()
#12 0x039a8879 in GSEventRunModal ()
#13 0x039a893e in GSEventRun ()
#14 0x00732a9b in UIApplicationMain ()
#15 0x00001e5b in main

如您所见,此调用堆栈不是很有帮助,因为它没有显示我的代码中的任何方法。此外,错误中显示的调用堆栈有 22 个内存地址,而来自线程 1 的堆栈只有 15 个,而且地址根本不匹配。似乎没有其他线程包含任何有用的信息。

如何从错误(具有 22 个地址的那个)中看到“第一次抛出调用堆栈”的线程,所以我可以找到导致此错误的行?也许我的构建设置中有一些设置不正确,导致相关堆栈无法检索?

如果有人能指出我正确的方向,我将不胜感激。试图手动定位有问题的线路是相当乏味的。

谢谢!

4

3 回答 3

17

打开调试器并在抛出异常时设置断点,这样您就可以准确地知道哪一行代码是一个混蛋。

objc_exception_throw

或者,[NSException raise];

看看下面的问题:Add breakpoint to objc-exception-throw

于 2011-11-23T09:21:06.430 回答
2

Did you enable global breakpoints in your project? if not add objc_exception_throw to the breakpoints section in the project navigator then re-run the app, you should get the stack. In adition, when you crash happends, watch and expand any additional threads to see their stacks as well. It happened to me several times that the stack i was searching was in a background thread, though the crash was being reported by the main thread. HTH.

于 2011-11-23T09:26:00.523 回答
0

Assuming this happened while in the XCode debugger, you can determine the line of code referenced in the traceback addresses. In the debug window, enter the following:

list *address from traceback

For example, for your first entry, you would enter the following: list *0x2263052

Do this for each address. One of them should point to your programs code and list the failing line number and code above and below the failing line.

-Steve

于 2015-02-05T16:45:42.207 回答