7

I found this via a crash report:

*** -[__NSArrayM objectAtIndex:]: index 9223372036854775807 beyond bounds [0 .. 8]
CoreFoundation-[__NSArrayM removeObjectAtIndex:]

This is the code responsible:

[self someMethodwithCallback:^(NSMutableArray *stickerArray)
{
   _tableViewController.tableView beginUpdates];

   _layersArray = stickerArray;

   NSArray *reversed = [[_layersArray reverseObjectEnumerator] allObjects];

   _layersArray = [reversed mutableCopy];

  [_tableViewController.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];

  [_tableViewController.tableView endUpdates];

  [_tableViewController.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];

  [_delegate stickerSelectedAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
}

How and where would this huge index be happening?

daihovey
  • 3,485
  • 13
  • 66
  • 110
  • 9
    Well obviously your 9223372036854775807th object in the array is missing. Make sure you have at least 9223372036854775808 objects in your data source. On a more serious note, [it's the highest 64-bit number](http://en.wikipedia.org/wiki/9223372036854775807). Use breakpoints to figure out which line causes the crash. – Oscar Apeland Mar 13 '14 at 22:03
  • 9223372036854775807 looks very much like long max to me – David Rönnqvist Mar 13 '14 at 22:03
  • 1
    @OscarApeland Here's an interesting though. If 9223372036854775807 is long max, would the OP be able to store long max + 1 elements in an NSArray... interesting... – David Rönnqvist Mar 13 '14 at 22:05
  • @OscarApeland Theres only [0 .. 8] in the array - hence the confusion. – daihovey Mar 13 '14 at 22:05
  • @OscarApeland Its difficult to debug, its via a beta testers crash report and I cant reproduce. The line number was the the start of the completion block. – daihovey Mar 13 '14 at 22:07
  • 2
    @DavidRönnqvist 9223372036854775807 is long_max but `NSArray` indexes are unsigned, so yes, 9223372036854775808 is possible. ;-) – Léo Natan Mar 13 '14 at 22:07
  • 3
    The number 9223372036854775807 is 0x7FFFFFFFFFFFFFFF which represents -1 as a signed number. So it seems the index went below 0 somewhere. – Flovdis Mar 13 '14 at 22:11
  • @Flovdis Thats the point. Looking at the code, where could that have happened? All NSIndexPath's are hardcoded ... where else could it be happening? – daihovey Mar 13 '14 at 22:13
  • Nowhere in the method above are you removing objects from a mutable array. That's where your crash is happening. – jrturton Mar 13 '14 at 22:19
  • @daidai I suspect the `[_layersArray reverseObjectEnumerator]` part. But could you point out where exactly you get the exception, and probably the calling tree. – Flovdis Mar 13 '14 at 22:21
  • @jrturton exactly, thats why I'm confused. – daihovey Mar 13 '14 at 22:23
  • @Flovdis - 0x7ff...ff is not -1, it's the max positive value. 922...808, of course would be 0x800...00 and the largest magnitude negative number. – Hot Licks Mar 13 '14 at 22:52
  • 1
    I think we probably need to see the call stack. – nielsbot Mar 14 '14 at 00:12
  • 1
    I would note that nowhere in your listing is there an objectAtIndex or a removeObjectAtIndex call. I don't think you have the right code. (Vote to close for lack of information.) – Hot Licks Mar 14 '14 at 01:04
  • The question shouldn't be how and where this is happening, but what you can do to find where it is happening. In Xcode, under "Breakpoints", add an Exception Breakpoint. Change it to "Objective-C exception" if you like. Then you run your program, and it will stop exactly where the exception happened, so you can follow it back. – gnasher729 Mar 13 '14 at 23:24

1 Answers1

16

I believe the index in question is NSNotFound.

You're probably getting index from the result of a "find in array" operation (or the location of an NSRange returned from one)

nielsbot
  • 15,922
  • 4
  • 48
  • 73
  • 1
    It's sad that this was closed as "[lacking] sufficient information to diagnose the problem," since I googled the error message I got, "Index 9223372036854775807 out of bounds," and was taken right to this Stack Overflow question. The reason I did that is because I figured an odd number like that was probably not random. Thank you for your answer. – Mario Jan 09 '18 at 17:04