msetten
msetten

Reputation: 71

Crashes with [__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array error

I am getting crash reports via users of my iOS app, but crash I can't reproduce myself, nor can I trace the error back to a line in my own code (except that it originates from line 14 in main.m but this is the default application creation of iOS apps. Below the a crash report I received.

In understand that at some point a object is at index 0 is being retrieved from an empty Array. But since it doesn't seem to point to my own code, can it be a bug in iOS? (It happens on different platforms and with different iOS versions).

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

CRASH REPORT:

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)

Upvotes: 7

Views: 30912

Answers (4)

Michael Superczynski
Michael Superczynski

Reputation: 1537

This will stop the debugger at the offending line:

Xcode/Edit Scheme/Diagnostics/Log Exceptions.

Upvotes: 3

maz
maz

Reputation: 8336

One way to debug this issue is to add a Symbolic Breakpoint on objectAtIndex: This may result in many hits to objectAtIndex: before the debugger hits the one you're after but it's guaranteed to find it.

Steps in Xcode 4:

  • View > Navigators > Breakpoint Navigator
  • click '+' and choose Add Symbolic Breakpoint in the pop-up
  • type in "objectAtIndex:" in the Symbol text field and click Done

at runtime, if you can't tell which objectAtIndex: you're hitting, move the slider at the bottom of the Debug Navigator all the way to the right.

Upvotes: 20

alexantd
alexantd

Reputation: 3595

You need to search your project for all usages of objectAtIndex: and rule out each one as the culprit. Make sure you are never calling it on an empty array.

Another tip: If you know you only want to get the last object in an array, or you know the array will only ever contain one object, you can use lastObject instead of objectAtIndex: -- it's safer because it won't throw an exception, though you still need to check for nil.

Upvotes: 1

vikingosegundo
vikingosegundo

Reputation: 52227

This error means, that you try to get an object from a NSArray from a position where no object is found. In other words: if the index is 0, than your array is empty.
And most likely it is your code, as main() invokes your code.

Upvotes: 2

Related Questions