1
    *** Terminating app due to uncaught exception 'NSRangeException', reason: '
*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
    *** First throw call stack:
    (0x276b2fef 0x35ba6c8b 0x275c5841 0xe78bb 0xfe689 0x2b19e281 0x2b19ebab 0x2aedc981 0x2b19ea63 0x2b19f1b3 0x2afac2df 0x2b1943a7 0x2afa34d7 0x2aecd003 0x2a8eefc1 0x2a8ea7fd 0x2a8ea685 0x2a8ea031 0x2a8e9e1f 0x2a8e3b79 0x27678ffd 0x276766bb 0x27676ac3 0x275c3221 0x275c3033 0x2eff2201 0x2af2f8c9 0x114b59 0x36156aaf)
    libc++abi.dylib: terminating with uncaught exception of type NSException

I have many [NSArray objectAtIndex], I don't know which one makes it crash. I've writen the debug code below, but still can't catch it. It's in a UITableView, I load more and more cells by pull down the table, then it sometimes crashes.

    #import "NSArray+Debug.h"
#import "MLTool.h"

@implementation NSArray (Debug)

- (id)objectAtIndexEx:(NSUInteger)index{
    if (self.count<1) {
        assert(0);
    }

    NSString *str=[NSString stringWithFormat:@"count=%d,index=%d,info=%@",self.count,index,[self objectAtIndex:index]];
    if ([MLTool isEmptyString:str]
      //  ||str==
        ) {
        assert(0);
    }

    NSLogUTF8(@"break:%@",str);
    return [self objectAtIndex:index];
}
@end
Gank
  • 4,507
  • 4
  • 49
  • 45
  • 5
    Try using an exception breakpoint. It will pinpoint the line of code which is causing the crash. https://developer.apple.com/library/ios/recipes/xcode_help-breakpoint_navigator/articles/adding_an_exception_breakpoint.html – ZeMoon Mar 26 '15 at 08:19
  • @ZeMoon I've thousands of `objectAtIndex` in many loops, and I think it even not crash in the UITableView's source file, so I can't make an breakpoint. It crashes sometimes, and sometimes it's OK. – Gank Mar 26 '15 at 08:24
  • 2
    ZeMoon recommends you to set a special kind of breakpoint - All Exceptions. it stops execution exactly before the exception occures and shows you the line of code that causes the exception so you don't need to iterate through thousand stops. – heximal Mar 26 '15 at 08:31
  • Thanks all. I've solve it by your solution! http://stackoverflow.com/questions/19095150/symbolic-exception-breakpoint-on-nsrangeexception-raise – Gank Mar 26 '15 at 09:05
  • Or if your application is in app store already, you can use third party libraries to help you catch the crashes. for instance test flight or crashlytics – air_bob Mar 26 '15 at 09:35

1 Answers1

3

You can add an "exception breakpoint" in xcode to stop your debugging at the moment of the crash and check why it will crash.

For that in the section breakpoint/debug of the left navigator you can tap on the "+" at the bottom-left corner to add an "Exception breakpoint".