iosobjective-ccrashnsrangeexception

strange crash in iOS


There is a group of crashes reported by crittercism.

I've never reproduced it but there're about 50 times from different users in 1 weeks.

21
XXApp 0x0000000100129fb0 -[NSString(XXFormat) attributedStringFromHTMLByFont:] (NSString+XXFormat.m:15) was crashed.

Following is The line 15 code in NSString+XXFormat.m:

NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithData:[self dataUsingEncoding:NSUTF8StringEncoding]
                                                                                options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                                                                          NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
                                                                     documentAttributes:nil error:nil];

Name: NSRangeException

Reason: *** -[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 .. 2]

Stack:

0   
CoreFoundation 0x0000000183a0c2d8 __exceptionPreprocess + 128
1   
libobjc.A.dylib 0x00000001952380e4 objc_exception_throw + 56
2   
CoreFoundation 0x00000001838ef85c -[__NSArrayM objectAtIndex:] + 260
3   
UIKit 0x0000000188575a94 -[UITableView cellForRowAtIndexPath:] + 212
4   
UIKit 0x000000018873fa00 -[UITableViewWrapperView gestureRecognizerShouldBegin:] + 284
5   
UIKit 0x00000001885e2c34 -[UIGestureRecognizer _shouldBegin] + 1072
6   
UIKit 0x000000018847b340 -[UIGestureRecognizer setState:] + 428
7   
UIKit 0x00000001885f4fdc -[UIScrollViewPanGestureRecognizer touchesMoved:withEvent:] + 100
8   
UIKit 0x000000018847a6d8 -[UIWindow _sendGesturesForEvent:] + 592
9   
UIKit 0x0000000188479f50 -[UIWindow sendEvent:] + 656
10  
UIKit 0x000000018844d18c -[UIApplication sendEvent:] + 260
11  
UIKit 0x00000001886ee324 _UIApplicationHandleEventFromQueueEvent + 15420
12  
UIKit 0x000000018844b6a0 _UIApplicationHandleEventQueue + 1712
13  
CoreFoundation 0x00000001839c4240 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 20
14  
CoreFoundation 0x00000001839c359c __CFRunLoopDoSources0 + 444
15  
CoreFoundation 0x00000001839c1594 __CFRunLoopRun + 708
16  
CoreFoundation 0x00000001838ed2d4 CFRunLoopRunSpecific + 392
17  
UIFoundation 0x0000000191850c10 -[NSHTMLReader _loadUsingWebKit] + 1984
18  
UIFoundation 0x0000000191851fd0 -[NSHTMLReader attributedString] + 28
19  
UIFoundation 0x00000001917ef23c _NSReadAttributedStringFromURLOrData + 5800
20  
UIFoundation 0x00000001917edad8 -[NSAttributedString(NSAttributedStringUIFoundationAdditions) initWithData:options:documentAttributes:error:] + 144

21  
XXApp 0x0000000100129fb0 -[NSString(XXFormat) attributedStringFromHTMLByFont:] (NSString+XXFormat.m:15)

22  
XXApp 0x0000000100112f64 -[XXNews(Peer) attributedContent] (XXNews+Peer.m:56)
23  
XXApp 0x00000001000fc854 -[XXWidgetNewsCell setWidget:] (XXWidgetNewsCell.m:53)
24  
XXApp 0x0000000100116ba4 -[XXTimeLineTableViewController tableView:cellForRowAtIndexPath:] (XXTimeLineTableViewController.m:231)
25  
UIKit 0x000000018874da68 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 540
26  
UIKit 0x0000000188741890 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2360
27  
UIKit 0x000000018852d268 -[UITableView layoutSubviews] + 168
28  
UIKit 0x0000000188449760 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 576
29  
QuartzCore 0x0000000187d91e1c -[CALayer layoutSublayers] + 148
30  
QuartzCore 0x0000000187d8c884 CA::Layer::layout_if_needed() + 316
31  
QuartzCore 0x0000000187d8c728 CA::Layer::layout_and_display_if_needed() + 28
32  
QuartzCore 0x0000000187d8bebc CA::Context::commit_transaction() + 272
33  
QuartzCore 0x0000000187d8bc3c CA::Transaction::commit() + 524
34  
QuartzCore 0x0000000187d85364 CA::Transaction::observer_callback() + 76
35  
CoreFoundation 0x00000001839c42a4 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 28
36  
CoreFoundation 0x00000001839c1230 __CFRunLoopDoObservers + 356
37  
CoreFoundation 0x00000001839c1610 __CFRunLoopRun + 832
38  
CoreFoundation 0x00000001838ed2d4 CFRunLoopRunSpecific + 392
39  
GraphicsServices 0x000000018d1036fc GSEventRunModal + 164
40  
UIKit 0x00000001884b2fac UIApplicationMain + 1484
41  
XXApp 0x0000000100101280 main (main.m:14)
42  
libdyld.dylib 0x00000001958b6a08 start + 0

Solution

  • It's not the snippet that you have shared here that is causing the issue but you are trying to access to in-existent element in an array :

    Name: NSRangeException
    
    Reason: *** -[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 .. 2]
    

    the array has bounds [0 .. 2] and you try to access to index 3 that's not existent element

    make sure in your method cellForRowAtIndexPath you are not bypassing the index of your NSArray