Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysterious crash with NSString method rangeOfComposedCharacterSequenceAtIndex:

My app uses several UITextViews, and we are getting a crash report from users that we are unable to reproduce.

The crash report doesn’t (seem to) contain any of our code, and crashes with an NSInvalidArgumentException in the NSString method rangeOfComposedCharacterSequenceAtIndex:, which isn't called directly by our code but seems to be called by the frameworks.

Here is the crash report:

0  CoreFoundation    __exceptionPreprocess + 130
1 libobjc.A.dylib    objc_exception_throw + 38
2  CoreFoundation    -[NSException initWithCoder:]
3  Foundation        -[NSString rangeOfComposedCharacterSequenceAtIndex:] + 88
4  UIKit             __74-[UITextInputController _validCaretPositionFromCharacterIndex:downstream:]_block_invoke + 328
5  UIFoundation      -[NSTextStorage coordinateReading:] + 36
6  UIKit             -[UITextInputController _validCaretPositionFromCharacterIndex:downstream:] + 218
7  UIKit             __52-[UITextInputController _characterPositionForPoint:]_block_invoke + 1112
8  UIFoundation      -[NSLayoutManager(TextLocking) coordinateAccess:] + 46
9  UIKit             -[UITextInputController _characterPositionForPoint:] + 224
10 UIKit             -[UITextSelection setSelectionWithFirstPoint:secondPoint:] + 56
11 UIKit             -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) twoFingerRangedSelectGesture:] + 386
12 UIKit             _UIGestureRecognizerSendActions + 196
13 UIKit             -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 1138
14 UIKit             ___UIGestureRecognizerUpdate_block_invoke + 48
15 UIKit             _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 218
16 UIKit             _UIGestureRecognizerUpdate + 282
17 CoreFoundation    __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 20
18 CoreFoundation    __CFRunLoopDoObservers + 284
19 CoreFoundation    __CFRunLoopRun + 730
20 CoreFoundation    CFRunLoopRunSpecific + 522
21 CoreFoundation    CFRunLoopRunInMode + 106
22 GraphicsServices  GSEventRunModal + 138
23 UIKit             UIApplicationMain + 1136
24 main.m line 16 
25 libdyld.dylib     start + 2

( https://gist.github.com/timarnold/6981caa6a1ee2b98c2fe )

Since I don’t know exactly what part of our code is causing the crash, I’m not sure what sample code would be helpful to post. I’m more interested in hearing if anyone has seen anything like this, or might have a suggestion about where or how to investigate this further.

Update on 2013-11-21

I was able to reproduce this issue by doing the following:

  • Add some text to my UITextView, including a trailing newline character (\n)
  • Get my app to force the UITextView to resign first responder status
  • Get my app to assign first responder status to my UITextView, and then tap to insert the cursor at the end of the string (around the location of the newline character)

after which the app crashed with the above report.

I attempted to create this in an empty Xcode project with just a stock UITextView and nothing else, and was unable to do so. It seems as though there's something going on in my app that conspires with UITextView to make this crash occur. Would love to know what, but the issue is solved for me in this project (as we are not interested in trailing newline characters, and can trim them, thus keeping crashes from occurring).

If someone can reproduce this in a sample project, would be great to file a radar if this is indeed a bug with UITextView.

Thanks to @wattson12 and @Johan Kool for responses leading to a solution.

like image 820
Tim Arnold Avatar asked Nov 18 '13 16:11

Tim Arnold


2 Answers

I had the same problem today, I don't have an old device but would be interesting to see if this is different on < 7.0.3 since its very reproducible now and wasn't picked up before. I can't really explain the why, but here is what I noticed and the fix I used:

I saw that tapping on a text view was crashing when:

  • I had a new line character at the end of the text, and
  • I tapped somewhere outside the current text

My solution was to trim the newline from the end (I used a while loop to avoid removing any leading newline characters but stringByTrimmingCharactersInSet would be fine if thats not an issue)

like image 108
wattson12 Avatar answered Oct 14 '22 16:10

wattson12


Setting scrollEnabled to YES (default) fixed the crash for us. We needed scrollEnabled to be NO for the UI. But an suboptimal UI is better then a crash so this category method enable it only for iOS versions below 7.1

- (void)enableScrollIfBuggyOSVersion {
    if ([[[UIDevice currentDevice] systemVersion] compare:@"7.1" options:NSNumericSearch] == NSOrderedAscending) {
        [self setScrollEnabled:YES];
    } else {
        [self setScrollEnabled:NO];
    }
}
like image 35
Joakim Gyllström Avatar answered Oct 14 '22 15:10

Joakim Gyllström