index 0 beyond bounds for empty array error - iphone

I don't understand on how to debug this error message:
2011-02-01 20:45:56.151 NeMe[3206:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'
*** Call stack at first throw:
(
0 CoreFoundation 0x027deb99 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x0292e40e objc_exception_throw + 47
2 CoreFoundation 0x027d4695 -[__NSArrayM objectAtIndex:] + 261
3 NeighborMe 0x0000f617 -[NeighborMapViewController regionFromLocations] + 65
4 NeighborMe 0x0001047b -[NeighborMapViewController locationManager:didUpdateToLocation:fromLocation:] + 94
5 CoreLocation 0x02393870 -[CLLocationManager onClientEventLocation:] + 793
6 CoreLocation 0x0239218b OnClientEvent + 49
7 CoreLocation 0x023a8a83 _Z22CLClientInvokeCallbackP10__CLClient13CLClientEventPK14__CFDictionary + 47
8 CoreLocation 0x023a9b2c _Z27CLClientHandleDaemonDataFixP10__CLClientPK23CLDaemonCommToClientFixPK14__CFDictionary + 290
9 CoreLocation 0x023acb30 _Z24CLClientHandleDaemonDataP12__CFMachPortPvlS1_ + 1125
10 CoreFoundation 0x02720982 __CFMachPortPerform + 338
11 CoreFoundation 0x027bfff4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
12 CoreFoundation 0x02720807 __CFRunLoopDoSource1 + 215
13 CoreFoundation 0x0271da93 __CFRunLoopRun + 979
14 CoreFoundation 0x0271d350 CFRunLoopRunSpecific + 208
15 CoreFoundation 0x0271d271 CFRunLoopRunInMode + 97
16 GraphicsServices 0x030bd00c GSEventRunModal + 217
17 GraphicsServices 0x030bd0d1 GSEventRun + 115
18 UIKit 0x002eeaf2 UIApplicationMain + 1160
19 NeighborMe 0x00002818 main + 102
20 NeighborMe 0x000027a9 start + 53
21 ??? 0x00000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'
There is NSMutableArray in this code... so how is this possible?

You have an empty array. You tried to call [array objectAtIndex:0]. 0 is beyond the bounds of an empty array (not surprising – anything is beyond the bounds for an empty array).

It means that early in [NeighborMapViewController regionFromLocations] you are attempting to index an NSMutableArray object. That array has zero elements in it -- it's empty. But you're apparently trying to access index 0, which would be the first element. Since there is no first element, you get an exception.
Probably the code that you expect to have executed to add items to the array has not yet executed, or you simply need to check the length of the array prior to trying to access the element at index 0. It's hard to say without knowing more about what you're doing.
gdb reports the offset as 65 bytes in to the function, so it's likely in one of the first few lines. You could probably manually inspect the code of the function to see, or set a breakpoint on the first line of the function and step through it.

I was seeing this error, however, not as an exception but only in the Issue Navigator. The solution was to simply restart XCode, then the error was gone.

As Kevin Ballard said, this error is thrown when you ask for the element at index X that is beyond array bounds, in addition to looking for [array objectAtIndex: X], this error may be even caused from the modern syntax array[X]

Related

Objective-C NSMutableArray:removeObjectsInRange

I have a NSMutableArray called rawData of size 198792. Each index contains an NSObject called DataSet. The interface for a DataSet is as follows:
#interface DataSet : NSObject {
NSNumber *rightFoot;
NSNumber *leftFoot;
}
I am trying to trim down the rawData using the following line of code:
[rawData removeObjectsInRange:NSMakeRange(0, StartTime*freq-1)];
where StartTime*freq-1 = 11799.
I am getting an error during runtime:
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSMutableArray objectAtIndex:]: index 198791 beyond bounds [0 .. 186992]'
Thank you for any help!
EDIT: stack trace
2012-02-16 17:59:31.671 fwd_analysis[8154:207] *** Terminating app due to uncaught
exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index
198791 beyond bounds [0 .. 186992]'
*** Call stack at first throw:
(
0 CoreFoundation 0x00dc25a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f16313 objc_exception_throw + 44
2 CoreFoundation 0x00db80a5 -[__NSArrayM objectAtIndex:] + 261
3 fwd_analysis 0x00002a99 -[fwd_analysisViewController startButtonPressed:] + 252
4 UIKit 0x002b24fd -[UIApplication sendAction:to:from:forEvent:] + 119
5 UIKit 0x00342799 -[UIControl sendAction:to:forEvent:] + 67
6 UIKit 0x00344c2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
7 UIKit 0x003437d8 -[UIControl touchesEnded:withEvent:] + 458
8 UIKit 0x002d6ded -[UIWindow _sendTouchesForEvent:] + 567
9 UIKit 0x002b7c37 -[UIApplication sendEvent:] + 447
10 UIKit 0x002bcf2e _UIApplicationHandleEvent + 7576
11 GraphicsServices 0x0171a992 PurpleEventCallback + 1550
12 CoreFoundation 0x00da3944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
13 CoreFoundation 0x00d03cf7 __CFRunLoopDoSource1 + 215
14 CoreFoundation 0x00d00f83 __CFRunLoopRun + 979
15 CoreFoundation 0x00d00840 CFRunLoopRunSpecific + 208
16 CoreFoundation 0x00d00761 CFRunLoopRunInMode + 97
17 GraphicsServices 0x017191c4 GSEventRunModal + 217
18 GraphicsServices 0x01719289 GSEventRun + 115
19 UIKit 0x002c0c93 UIApplicationMain + 1160
20 fwd_analysis 0x00002758 main + 102
21 fwd_analysis 0x000026e9 start + 53
)
terminate called after throwing an instance of 'NSException'
Current language: auto; currently objective-c
Program received signal: “SIGABRT”.
Your array can't be the size you think it is.
Look at the error message: index beyond bounds [0 .. 186992]. Therefore the size of your array is actually 186993.
I would also recommend you log the value of StartTime*freq-1 at runtime, and compare it with the length of your array.
Also, something that could cause issues here is that it's a mutable array, and you're removing stuff from it, so the length will change at runtime. This is why I recommend logging both the length and range at the point where the code breaks.
So, something like this:
NSLog(#"Length: %i",[rawData length]);
NSLog(#"Range: %i",StartTime*freq-1);
[rawData removeObjectsInRange:NSMakeRange(0, StartTime*freq-1)];
Update: how to add objects to another array instead of deleting from the current one.
You need to use this method to get an array which contains objects in a certain range:
- (NSArray *)subarrayWithRange:(NSRange)range
You can then add these objects into another array, for example:
// before you did this:
[rawData removeObjectsInRange:NSMakeRange(0, StartTime*freq-1)];
// to create a filtered array do:
NSArray *filtered = [rawData subarrayWithRange:NSMakeRange(0, StartTime*freq-1)];

NSRangeException exception on NSMutableArray

I am getting the following error please help me with a solution. My xcode gets aborted with the following error
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'
*** Call stack at first throw:
(
0 CoreFoundation 0x0140a5a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x0155e313 objc_exception_throw + 44
2 CoreFoundation 0x014000a5 -[__NSArrayM objectAtIndex:] + 261
3 UMMCelebrity 0x00043903 -[UMMFlickrView photoGallery:urlForPhotoSize:atIndex:] + 115
4 UMMCelebrity 0x0004a9d6 -[FGalleryViewController createGalleryPhotoForIndex:] + 422
5 UMMCelebrity 0x0004a5ac -[FGalleryViewController loadThumbnailImageWithIndex:] + 172
6 UMMCelebrity 0x0004a189 -[FGalleryViewController preloadThumbnailImages] + 281
7 UMMCelebrity 0x00046f84 -[FGalleryViewController loadView] + 2132
8 UIKit 0x0029d00e -[UIViewController view] + 56
9 UMMCelebrity 0x00043050 -[UMMFlickrView pushphotoview] + 192
10 UMMCelebrity 0x000446c6 -[UMMFlickrView connection:didReceiveData:] + 2230
11 Foundation 0x00996835 _NSURLConnectionDidReceiveData + 159
12 CFNetwork 0x033bde72 _ZN19URLConnectionClient21_clientDidReceiveDataEPK8__CFDataPNS_26ClientConnectionEventQueueE + 262
13 CFNetwork 0x0348a6b3 _ZN19URLConnectionClient26ClientConnectionEventQueue33processAllEventsAndConsumePayloadEP20XConnectionEventInfoI12XClientEvent18XClientEventParamsEl + 247
14 CFNetwork 0x0348a9cf _ZN19URLConnectionClient26ClientConnectionEventQueue33processAllEventsAndConsumePayloadEP20XConnectionEventInfoI12XClientEvent18XClientEventParamsEl + 1043
15 CFNetwork 0x033b5c80 _ZN19URLConnectionClient13processEventsEv + 100
16 CFNetwork 0x033b5acf _ZN17MultiplexerSource7performEv + 251
17 CoreFoundation 0x013eb8ff __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
18 CoreFoundation 0x0134988b __CFRunLoopDoSources0 + 571
19 CoreFoundation 0x01348d86 __CFRunLoopRun + 470
20 CoreFoundation 0x01348840 CFRunLoopRunSpecific + 208
21 CoreFoundation 0x01348761 CFRunLoopRunInMode + 97
22 GraphicsServices 0x018e21c4 GSEventRunModal + 217
23 GraphicsServices 0x018e2289 GSEventRun + 115
24 UIKit 0x001fbc93 UIApplicationMain + 1160
25 UMMCelebrity 0x00001d29 main + 121
26 UMMCelebrity 0x00001ca5 start + 53
)
terminate called after throwing an instance of 'NSException'
You're calling objectAtIndex: with the argument 0 on an empty NSMutableArray.
[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array
If you know the NSMutableArray can be empty, you should consider calling count on it to see if it does indeed contain any elements, before trying to access the elements.
Obviously, there's an empty NSMutableArray in -[UMMFlickrView photoGallery:urlForPhotoSize:atIndex:] and you sent it objectAtIndex: with argument 0.
That means your NSMutableArray is empty. I think you didn't allocate the memory for NSMutableArray. Read this link, it may be helpful for you.
http://www.roseindia.net/answers/viewqa/Mobile-Applications/14614-NSMutableArray-Example-Code.html
May be on the 4th click nothing is pushed into the array, I'm not sure as I have no idea what you are trying to do.If you can add the code line that adds the object into your array, we could help you.
This error is occurring because the array is empty for sure.

Why sometimes my application stops working and gives Program received signal: “SIGABRT” error?

I have four views in my navigational based application. MainView, AddView, ShowView and DetailView. MainView has a NSMutableArray. When i click on button add, then i go to AddView and then i add an object in NSMutable array of MainView. Then i come back and go to ShowView which is a tableView. From MainView i am calling createList function of ShowView like this:
ShowView *show = [[ShowView alloc] init];
[show createList: self.savedObjectsList];
[self.navigationController pushViewController: show animated: YES];
[runListController release];
In ShowView createList looks like this:
- (void) createRunsList: (NSMutableArray *) list{
objList = [list retain];
}
where objList is NSMutableArray in ShowView. every cell of table view creates a DetailView of an object of NSMutbaleArray. Problem is sometimes my applications stop working and i get this error:
2011-10-03 15:35:55.076 RunnoIPhoneApp[2750:707] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'
*** Call stack at first throw:
(
0 CoreFoundation 0x3399964f __exceptionPreprocess + 114
1 libobjc.A.dylib 0x30b16c5d objc_exception_throw + 24
2 CoreFoundation 0x33904069 -[__NSArrayM objectAtIndex:] + 184
3 RunnoIPhoneApp 0x0000b79f -[PostRunDetailViewController createRunDetail:] + 90
4 RunnoIPhoneApp 0x0000fd2f -[RunListViewController tableView:didSelectRowAtIndexPath:] + 182
5 UIKit 0x3203f51b -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 662
6 UIKit 0x320a30eb -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 130
7 Foundation 0x32ba26d5 __NSFireDelayedPerform + 368
8 CoreFoundation 0x33970a47 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 14
9 CoreFoundation 0x33972ecb __CFRunLoopDoTimer + 850
10 CoreFoundation 0x33973845 __CFRunLoopRun + 1088
11 CoreFoundation 0x33903ec3 CFRunLoopRunSpecific + 230
12 CoreFoundation 0x33903dcb CFRunLoopRunInMode + 58
13 GraphicsServices 0x3162e41f GSEventRunModal + 114
14 GraphicsServices 0x3162e4cb GSEventRun + 62
15 UIKit 0x32019d69 -[UIApplication _run] + 404
16 UIKit 0x32017807 UIApplicationMain + 670
17 RunnoIPhoneApp 0x00002553 main + 70
18 RunnoIPhoneApp 0x00002508 start + 40
)
terminate called after throwing an instance of 'NSException'
Program received signal: “SIGABRT”.
warning: Unable to read symbols for /Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.3.5 (8L1)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib (file not found).
Program received signal: “SIGABRT”.
(gdb)
Can anybody tell me why is it happening? DetailView of some first objects works fine but then i get this error. Thanks in advance.
Looking at the stack trace, the exception is thrown in your [PostRunDetailViewController createRunDetail:] method, which is called when you select a row in your RunListViewController's UITableView. You are trying to access an element in the array that is out of bounds.
Put a break point at the start of this method and step through it in the debugger. Work out which array access is causing the exception and then think why the array might not contain the elements that you expect.
Just comment this line
[runListController release];

Crash in tableview cell

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 18 beyond bounds [0 .. 16]'
*** Call stack at first throw:
(
0 CoreFoundation 0x02b11b99 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x02c6140e objc_exception_throw + 47
2 CoreFoundation 0x02b07695 -[__NSArrayM objectAtIndex:] + 261
3 MyPocket 0x0005efe9 -[loginLocalitems tableView:didSelectRowAtIndexPath:] + 638
4 UIKit 0x00be9a48 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1140
5 UIKit 0x00be032e -[UITableView _userSelectRowAtIndexPath:] + 219
6 Foundation 0x0037821a __NSFireDelayedPerform + 441
7 CoreFoundation 0x02af2f73 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19
8 CoreFoundation 0x02af45b4 __CFRunLoopDoTimer + 1364
9 CoreFoundation 0x02a50dd9 __CFRunLoopRun + 1817
10 CoreFoundation 0x02a50350 CFRunLoopRunSpecific + 208
11 CoreFoundation 0x02a50271 CFRunLoopRunInMode + 97
12 GraphicsServices 0x03f4e00c GSEventRunModal + 217
13 GraphicsServices 0x03f4e0d1 GSEventRun + 115
14 UIKit 0x00b84af2 UIApplicationMain + 1160
15 MyPocket 0x000023d2 main + 84
16 MyPocket 0x00002375 start + 53
17 ??? 0x00000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'
What is the reason for the above array. This crash appears only when i select the lower cells of table view Anyone pls help
This error is telling you that your NSMutableArray has only from 0 to 16 objects inside. When you scroll to the bottom of the table and select the lower cell you are trying to access an object in the array at index higher then 16. So try to check if the array that you are using for the table is properly filled, and that you are not removing something from it at later time.
you are loading data from a mutable array?
check if the array really has 18 elements.
or if you are giving a number bigger than the actual row count in the numberOfRows inSection method
this is a range exception:
index 18 beyond bounds [0 .. 16]
as you see, it only occurs when you click the lower items

iPhone SDK / iOS crash in webview / scrollview?

I'm having really strange crash in my application. It occurs randomly. Basically, I'm having large scroll view containing multiple UIWebViews, which I'm loading from HTML string. After some time, following crash appears:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 7 beyond bounds [0 .. 6]'
*** Call stack at first throw:
(
0 CoreFoundation 0x0110dbe9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f025c2 objc_exception_throw + 47
2 CoreFoundation 0x011036e5 -[__NSArrayM objectAtIndex:] + 261
3 CoreFoundation 0x010866e5 -[NSMutableArray removeObject:range:identical:] + 437
4 CoreFoundation 0x01086520 -[NSMutableArray removeObject:] + 96
5 UIKit 0x0035e7c0 -[UIView(UIViewGestures) removeGestureRecognizer:] + 112
6 UIKit 0x005f85c1 -[UIWebSelectionAssistant setGestureRecognizers] + 58
7 UIKit 0x004b0d09 -[UIWebDocumentView loadHTMLString:baseURL:] + 286
8 CoreFoundation 0x0107e67d __invoking___ + 29
9 CoreFoundation 0x0107e551 -[NSInvocation invoke] + 145
10 WebCore 0x0217f3c3 _ZL15HandleAPISourcePv + 147
11 CoreFoundation 0x010ef01f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
12 CoreFoundation 0x0104d28b __CFRunLoopDoSources0 + 571
13 CoreFoundation 0x0104c786 __CFRunLoopRun + 470
14 CoreFoundation 0x0104c240 CFRunLoopRunSpecific + 208
15 CoreFoundation 0x0104c161 CFRunLoopRunInMode + 97
16 WebCore 0x02240423 _ZL12RunWebThreadPv + 499
17 libSystem.B.dylib 0x901ea85d _pthread_start + 345
18 libSystem.B.dylib 0x901ea6e2 thread_start + 34
)
terminate called after throwing an instance of 'NSException'
I really can not resolve this, because crashes are random and seem to be in CoreFoundation, which I do not controll directly. Any suggestion?
Thank you very much for any help!
Not really an answer to your issue, but: be aware that Apple recommend against putting UIWebViews inside UIScrollViews. It's certainly possible to do, in my experience, but just be warned that wacky things might happen.
Related question:
Two resizable UIWebViews inside UIScrollView
The problem is in your gesture recognizers. The UIView is attempting to remove a recognizer that is presumably not there. It accesses index 7 of the 6 element array causing the crash.
I would check any customizations of the gestures you might have done.