Failure with [__NSArrayM objectAtIndex:]: index 0 is outside for an empty array error

I get crash reports through users of my iOS application, but I canโ€™t reproduce the crash myself, and I canโ€™t trace the error to a line in my own code (except that it arises from line 14 in main.m but this application is for the default iOS app below the crash report I received.

In the understanding that at some point the object is at index 0, it is retrieved from an empty array. But since this does not seem to indicate my own code, could this be a bug in iOS? (This happens on different platforms and with different versions of iOS).

I hope someone has an idea what is happening or can point me in the right direction. Thanks.

REPORT MESSAGE:

Incident Identifier: [TODO] CrashReporter Key: [TODO] Process: Mary Black [797] Path: /var/mobile/Applications/28A68F8B-294E-4B86-9E75-ED5484E5EF4D/Mary Black.app/Mary Black Identifier: net.broset.Mary-Black Version: 225 Code Type: ARM (Native) Parent Process: launchd [1] Date/Time: 2011-10-14 03:47:32 +0000 OS Version: iPhone OS 5.0 (9A334) Report Version: 104 Exception Type: SIGTRAP Exception Codes: #0 at 0x35b07848 Crashed Thread: 0 Application Specific Information: *** Terminating app due to uncaught exception \\\'NSRangeException\\\', reason: \\\'*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array\\\' Thread 0 Crashed: 0 libsystem_kernel.dylib 0x00010848 __kill + 8 1 CoreFoundation 0x000b9987 __handleUncaughtException + 75 2 libobjc.A.dylib 0x000092d1 _objc_terminate + 129 3 libc++abi.dylib 0x000043c5 _ZL19safe_handler_callerPFvvE + 77 4 libc++abi.dylib 0x00004451 operator delete(void*) + 1 5 libc++abi.dylib 0x00005825 __cxa_current_exception_type + 1 6 libobjc.A.dylib 0x00009235 objc_exception_rethrow + 13 7 CoreFoundation 0x0000f545 CFRunLoopRunSpecific + 405 8 CoreFoundation 0x0000f3a5 CFRunLoopRunInMode + 105 9 GraphicsServices 0x00003fed GSEventRunModal + 157 10 UIKit 0x00031743 UIApplicationMain + 1091 11 Mary Black 0x00002fa7 main (main.m:14) 
+7
source share
4 answers

One way to debug this problem is to add a Symbolic Breakpoint to objectAtIndex: this can lead to a lot of calls to the AtIndex object: before the debugger gets to the one you need, but it will definitely find it.

Steps in Xcode 4:

  • View> Navigators> Breakpoint Navigator
  • press "+" and select "Add Symbolic Breakpoint" in the popup window
  • enter "objectAtIndex:" in the "Symbol" text box and click "Finish"

at run time, if you cannot determine which AtIndex object: you click, move the slider at the bottom of the debug navigator all the way to the right.

+20
source

This will stop the debugger on the violation line:

Xcode / Edit Scheme / Diagnostics / Log Exceptions.

+3
source

This error means that you are trying to get an object from NSArray from a position where the object was not found. In other words: if the index is 0, your array is empty.
And most likely this is your code, since main () calls your code.

+2
source

You need to look for your project for all kinds of uses of objectAtIndex: and exclude each of them as the culprit. Make sure you never call it on an empty array.

One more tip: if you know that you want to get only the last object in the array or you know that the array will contain only one object, you can use lastObject instead of objectAtIndex: - it is safer because it will not throw an exception, although you donโ€™t care need to check nil .

+1
source

All Articles