textViewDidChange: crashes in iOS 7 - objective-c

As we know the UITextView is more powerful in iOS 7, but here it performs more fragile. The following code works fine when inputting "rr" with Chinese input keyboard in iOS 6, but crashes in iOS 7.
- (void)viewDidLoad
{
[super viewDidLoad];
self.tv = [[UITextView alloc] initWithFrame:CGRectMake(0, 100, 320, 100)];
self.tv.backgroundColor = [UIColor yellowColor];
self.tv.textColor = [UIColor blackColor];
self.tv.editable = YES;
self.tv.delegate = self;
[self.view addSubview:self.tv];
}
-(void)textViewDidChange:(UITextView *)textView{
int maxLength = 2;
if (self.tv.text.length > maxLength) {
NSLog(#"self.tv.text :%#",self.tv.text);
NSLog(#"self.tv.text.length :%d",self.tv.text.length);
NSLog(#"maxLength :%d",maxLength);
self.tv.text = [self.tv.text substringToIndex:maxLength];
}
}
The log is as following:
2013-11-13 15:48:16.003 Test2[1388:70b] self.tv.text :r r
2013-11-13 15:48:16.004 Test2[1388:70b] self.tv.text.length :3
2013-11-13 15:48:16.005 Test2[1388:70b] maxLength :2
2013-11-13 15:48:16.032 Test2[1388:70b] *** Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray replaceObjectsInRange:withObject:length:: Out of bounds'
*** First throw call stack:
(
0 CoreFoundation 0x017355e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x014b88b6 objc_exception_throw + 44
2 CoreFoundation 0x017353bb +[NSException raise:format:] + 139
3 Foundation 0x010ebba1 -[NSMutableRLEArray replaceObjectsInRange:withObject:length:] + 136
4 Foundation 0x010ebb14 -[NSMutableRLEArray deleteObjectsInRange:] + 63
5 Foundation 0x010ea559 -[NSConcreteMutableAttributedString replaceCharactersInRange:withAttributedString:] + 324
6 UIFoundation 0x02d9d9f4 __71-[NSConcreteTextStorage replaceCharactersInRange:withAttributedString:]_block_invoke + 68
7 UIFoundation 0x02d9d92f -[NSConcreteTextStorage replaceCharactersInRange:withAttributedString:] + 121
8 UIKit 0x00924d22 __53-[UITextInputController setMarkedText:selectedRange:]_block_invoke + 352
9 UIFoundation 0x02d9b491 -[NSTextStorage coordinateEditing:] + 48
10 UIKit 0x00924a38 -[UITextInputController setMarkedText:selectedRange:] + 249
11 UIKit 0x008fb3b1 -[UITextView setMarkedText:selectedRange:] + 63
12 UIKit 0x00644aa5 -[UIResponder(UITextInput_Internal) _setMarkedText:selectedRange:] + 101
13 UIKit 0x004031aa -[UIKeyboardImpl setMarkedText:selectedRange:inputString:searchString:] + 365
14 UIKit 0x00419737 -[TIKeyboardOperationSetMarkedText(UIKeyboardImpl) main] + 178
15 Foundation 0x0118da69 -[__NSOperationInternal _start:] + 671
16 Foundation 0x0110a798 -[NSOperation start] + 83
17 UIKit 0x0040e08a -[UIKeyboardImpl updateCandidateDisplayAsyncWithCandidateSet:documentOperation:] + 188
18 UIKit 0x00419041 -[TIKeyboardOperationSetCandidates(UIKeyboardImpl) main] + 112
19 Foundation 0x0118da69 -[__NSOperationInternal _start:] + 671
20 Foundation 0x0110a798 -[NSOperation start] + 83
21 UIKit 0x0040671d -[UIKeyboardImpl performOperations:] + 153
22 UIKit 0x00404f4e -[UIKeyboardImpl continueGenerateCandidatesAsynchronouslyWithOperations:] + 40
23 UIKit 0x00404c51 __87-[UIKeyboardImpl replyHandlerForGenerateCandidatesAsynchronouslyWithSelectedCandidate:]_block_invoke_2 + 46
24 UIKit 0x009381b8 -[UIKeyboardTaskQueue continueExecutionOnMainThread] + 402
25 UIKit 0x0093885f -[UIKeyboardTaskQueue addTask:] + 144
26 CoreFoundation 0x01729d1d __invoking___ + 29
27 CoreFoundation 0x01729c2a -[NSInvocation invoke] + 362
28 UIKit 0x008e63d2 -[_UIActionWhenIdle invoke] + 100
29 UIKit 0x008e64a6 __41-[_UIActionWhenIdle addObserverToRunLoop]_block_invoke + 36
30 CoreFoundation 0x0172924d _runLoopObserverWithBlockContext + 29
31 CoreFoundation 0x016fd53e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
32 CoreFoundation 0x016fd48f __CFRunLoopDoObservers + 399
33 CoreFoundation 0x016db3b4 __CFRunLoopRun + 1076
34 CoreFoundation 0x016dab33 CFRunLoopRunSpecific + 467
35 CoreFoundation 0x016da94b CFRunLoopRunInMode + 123
36 GraphicsServices 0x036d69d7 GSEventRunModal + 192
37 GraphicsServices 0x036d67fe GSEventRun + 104
38 UIKit 0x0022b94b UIApplicationMain + 1225
39 Test2 0x0000388d main + 141
40 libdyld.dylib 0x01d7370d start + 1
41 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
In addition, I test the following code, it also crashes after input "rr" with Chinese input keyboard.
-(void)textViewDidChange:(UITextView *)textView{
int maxLength = 2;
if (self.tv.text.length > maxLength) {
self.tv.text = #"c";
}
}
Any help will be appreciated!

What's happening is that you're typing what is referred to as multistage text input, i.e. the input has to be confirmed from the user before it's actually committed into the underlying text. You get the crash because the text is only kind-of inputted, and until the text has been committed, it could easily change to something else.
To detect this situation, you're looking for the markedTextRange property of the UITextView, which indicates if you're in this complex input mode.
If the property is non-nil, then you're in this special input mode, so you should guard your modification code with something like:
if (self.tv.markedTextRange == nil && self.tv.text.length > maxLength) {
// Perform change
}
Near as I can tell the crash is triggered by the special multistage text input mode, so you should avoid changing the text while this mode is active.

Related

Line Counting Function no longer works in Swift 3.0

I had a function that I was using in Swift 2.2 that I never had any problems with, now I'm getting an error when I try to use it.
The code is to change the number of lines a UILabel has to accommodate the size of the string.
func countLabelLines(label: UILabel) -> Int {
if let text = label.text {
let myText = text as NSString
let attributes = [NSFontAttributeName: label.font]
let width = label.bounds.width
let height = CGFloat.greatestFiniteMagnitude
let size = CGSize(width: width, height: height)
let labelSize = myText.boundingRect(with: size, options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: attributes, context: nil) //Code errors here
let lines = ceil(CGFloat(labelSize.height) / label.font.lineHeight)
return Int(lines)
}
return 0
}
And this is my error message:
> 2016-10-14 09:47:37.213 EZ Lists[23136:1997134] -[_SwiftValue pointSize]: unrecognized selector sent to instance 0x608000247380
2016-10-14 09:47:37.218 EZ Lists[23136:1997134] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue pointSize]: unrecognized selector sent to instance 0x608000247380'
*** First throw call stack:
(
0 CoreFoundation 0x000000010e2ef34b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010d93321e objc_exception_throw + 48
2 CoreFoundation 0x000000010e35ef34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x000000010e274c15 ___forwarding___ + 1013
4 CoreFoundation 0x000000010e274798 _CF_forwarding_prep_0 + 120
5 UIFoundation 0x000000011362a2ba __NSStringDrawingEngine + 3204
6 UIFoundation 0x0000000113629610 -[NSString(NSExtendedStringDrawing) boundingRectWithSize:options:attributes:context:] + 202
7 EZ Lists 0x000000010d2beaf2 _TZFC8EZ_Lists11CustomFuncs15countLabelLinesfT5labelCSo7UILabel_Si + 1218
8 EZ Lists 0x000000010d2be0f2 _TZFC8EZ_Lists11CustomFuncs21fitLabelTextForHeightfT5labelCSo7UILabel10constraintCSo18NSLayoutConstraint11extraHeightV12CoreGraphics7CGFloat_T_ + 82
9 EZ Lists 0x000000010d2ce445 _TFC8EZ_Lists9AddItemVC21viewDidLayoutSubviewsfT_T_ + 309
10 EZ Lists 0x000000010d2ce472 _TToFC8EZ_Lists9AddItemVC21viewDidLayoutSubviewsfT_T_ + 34
11 UIKit 0x000000010eee73a8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1337
12 QuartzCore 0x000000011478dcdc -[CALayer layoutSublayers] + 146
13 QuartzCore 0x00000001147817a0 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
14 QuartzCore 0x000000011478161e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
15 QuartzCore 0x000000011470f62c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280
16 QuartzCore 0x000000011473c713 _ZN2CA11Transaction6commitEv + 475
17 UIKit 0x000000010ee1c067 _UIApplicationFlushRunLoopCATransactionIfTooLate + 206
18 UIKit 0x000000010f62bb30 __handleEventQueue + 5672
19 CoreFoundation 0x000000010e294311 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
20 CoreFoundation 0x000000010e27959c __CFRunLoopDoSources0 + 556
21 CoreFoundation 0x000000010e278a86 __CFRunLoopRun + 918
22 CoreFoundation 0x000000010e278494 CFRunLoopRunSpecific + 420
23 GraphicsServices 0x0000000113f9da6f GSEventRunModal + 161
24 UIKit 0x000000010ee22f34 UIApplicationMain + 159
25 EZ Lists 0x000000010d2caa4f main + 111
26 libdyld.dylib 0x00000001120ce68d start + 1
27 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
I'm still pretty new at Xcode / Swift and I understand that one of my Arguments isn't right, but I don't understand which is the problem. After first I was thinking it was with the CGSize, but I broke that out into it's own line and that doesn't have an issue.
It's the font. The problem is this line:
let attributes = [NSFontAttributeName: label.font]
Try writing this:
let attributes = [NSFontAttributeName: label.font!]
I think the exclamation mark should fix it.

Getting error when retrieving iTunes Track Artwork image. Sometimes it says it's not an Image

I have the following code to retrieve the currentTrack's artwork (using iTunes.h):
iTunesArtwork *iTunesArtwork = [[iTunes.currentTrack artworks] objectAtIndex:0];
NSImage *artwork = iTunesArtwork.data;
if (artwork != nil){
[_musicImageView setImage:artwork];
}
It works for most of the tracks but sometimes it gives me this error (even when the track has an image):
NSImageCell's object value must be an NSImage.
2012-12-28 00:22:12.217 App[3256:303] (
0 CoreFoundation 0x00007fff904360a6 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff8d5bf3f0 objc_exception_throw + 43
2 CoreFoundation 0x00007fff90435e7c +[NSException raise:format:] + 204
3 AppKit 0x00007fff9747c31b -[NSImageCell setObjectValue:] + 106
4 AppKit 0x00007fff974b8563 -[NSImageView setImage:] + 90
5 App 0x0000000100001b38 -[AppDelegate setupPlayer] + 600
6 Foundation 0x00007fff8d10c513 __NSFireTimer + 96
7 CoreFoundation 0x00007fff903f2da4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
8 CoreFoundation 0x00007fff903f28bd __CFRunLoopDoTimer + 557
9 CoreFoundation 0x00007fff903d8099 __CFRunLoopRun + 1513
10 CoreFoundation 0x00007fff903d76b2 CFRunLoopRunSpecific + 290
11 HIToolbox 0x00007fff939e40a4 RunCurrentEventLoopInMode + 209
12 HIToolbox 0x00007fff939e3e42 ReceiveNextEventCommon + 356
13 HIToolbox 0x00007fff939e3cd3 BlockUntilNextEventMatchingListInMode + 62
14 AppKit 0x00007fff97337613 _DPSNextEvent + 685
15 AppKit 0x00007fff97336ed2 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 128
16 AppKit 0x00007fff9732e283 -[NSApplication run] + 517
17 AppKit 0x00007fff972d2cb6 NSApplicationMain + 869
18 App 0x0000000100001822 main + 34
19 libdyld.dylib 0x00007fff93cb77e1 start + 0
)
Any ideas on what's going on?
make sure it is an image -- not just check if its not nil, thats not enough. the type in an h file must not be the type the object really has. It is more like... it SHOULD be an image :D
if([artwork isKindOfClass:[NSImage class]]) {
....
from the comments.
That didnt always work. ell. no more crashes but some images didnt show. we used the rawData as fallback:
//kind of like this
if(![artwork isKindOfClass:[NSImage class]]) {
artwork = [[NSImage alloc] initWithData:tunesArtwork.rawData];
}
if([artwork isKindOfClass:[NSImage class]]) {
...
}

Thread 1 : signal SIGABRT - when trying to get NSMutableString

Here is my code:
.h
NSMutableString *buffer, *tmpBuffer;
int status; // 0=waiting for <DMX>, 1=recording
.m
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
buffer = [[NSMutableString alloc] init];
tmpBuffer = [[NSMutableString alloc] init];
status = 0;
}
- (void) receivedData:(NSString *)data {
// status=0 means we are still looking for start tag
if(status == 0) {
// add new data to last examined chunk (if any)
[tmpBuffer appendString:data];
// try to locate the open tag inside the tmpBuffer
NSRange range = [tmpBuffer rangeOfString:#"<DMX>" options:NSCaseInsensitiveSearch];
// if found, store the portion after the start tag into buffer
if(range.location != NSNotFound) {
range.length = [tmpBuffer length] - range.location + 5; // 5 is length of start tag...
[buffer setString:[tmpBuffer substringWithRange:range]];
status = 1; // set status to 1 so we know recording started
} else {
// store last examined chunk
[tmpBuffer setString:data];
}
} else {
[buffer appendString:data];
NSRange range = [buffer rangeOfString:#"</DMX>" options:NSCaseInsensitiveSearch];
if(range.location != NSNotFound) {
range.length = [buffer length] - range.location;
[self fullDMXReceived:buffer];
[buffer deleteCharactersInRange:range];
status = 0;
}
}
}
- (void) fullDMXReceived:(NSString*)finalData {
NSRunAlertPanel(#"", finalData, #"", #"", #"");
}
Here is the error:
2012-12-02 14:39:00.356 Chatty Mac[1805:303] *** Assertion failure in -[NSTextFieldCell _objectValue:forString:errorDescription:], /SourceCache/AppKit/AppKit-1187.34/AppKit.subproj/NSCell.m:1532
2012-12-02 14:39:00.362 Chatty Mac[1805:303] An uncaught exception was raised
2012-12-02 14:39:00.367 Chatty Mac[1805:303] Invalid parameter not satisfying: aString != nil
2012-12-02 14:39:00.374 Chatty Mac[1805:303] (
0 CoreFoundation 0x00007fff93e350a6 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff8f52e3f0 objc_exception_throw + 43
2 CoreFoundation 0x00007fff93e34ee8 +[NSException raise:format:arguments:] + 104
3 Foundation 0x00007fff933fe6a2 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 189
4 AppKit 0x00007fff92660205 -[NSCell _objectValue:forString:errorDescription:] + 159
5 AppKit 0x00007fff9266015f -[NSCell _objectValue:forString:] + 20
6 AppKit 0x00007fff926600db -[NSCell setStringValue:] + 39
7 AppKit 0x00007fff926f43fc -[NSControl setStringValue:] + 138
8 AppKit 0x00007fff928c2ba8 -[NSAlert buildAlertStyle:title:formattedMsg:first:second:third:oldStyle:] + 7479
9 AppKit 0x00007fff928c478b _NXDoLocalRunAlertPanel + 343
10 AppKit 0x00007fff928c461c NSRunAlertPanel + 157
11 Chatty Mac 0x00000001000070bd -[AppDelegate fullDMXReceived:] + 61
12 Chatty Mac 0x0000000100007036 -[AppDelegate receivedData:] + 582
13 Chatty Mac 0x000000010000e751 -[ORSSerialPortDemoController serialPort:didReceiveData:] + 401
14 Chatty Mac 0x000000010000a6fa -[ORSSerialPort receiveData:] + 170
15 Chatty Mac 0x0000000100009676 __block_global_1 + 38
16 libdispatch.dylib 0x00007fff9637bf01 _dispatch_call_block_and_release + 15
17 libdispatch.dylib 0x00007fff963780b6 _dispatch_client_callout + 8
18 libdispatch.dylib 0x00007fff9637d0c8 _dispatch_main_queue_callback_4CF + 275
19 CoreFoundation 0x00007fff93dd70fe __CFRunLoopRun + 1614
20 CoreFoundation 0x00007fff93dd66b2 CFRunLoopRunSpecific + 290
21 HIToolbox 0x00007fff949130a4 RunCurrentEventLoopInMode + 209
22 HIToolbox 0x00007fff94912e42 ReceiveNextEventCommon + 356
23 HIToolbox 0x00007fff94912cd3 BlockUntilNextEventMatchingListInMode + 62
24 AppKit 0x00007fff92682613 _DPSNextEvent + 685
25 AppKit 0x00007fff92681ed2 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 128
26 AppKit 0x00007fff92679283 -[NSApplication run] + 517
27 AppKit 0x00007fff9261dcb6 NSApplicationMain + 869
28 Chatty Mac 0x00000001000056d2 main + 34
29 Chatty Mac 0x0000000100001da4 start + 52
30 ??? 0x0000000000000003 0x0 + 3
)
2012-12-02 14:39:00.475 Chatty Mac[1805:303] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: aString != nil'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff93e350a6 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff8f52e3f0 objc_exception_throw + 43
2 CoreFoundation 0x00007fff93e34ee8 +[NSException raise:format:arguments:] + 104
3 Foundation 0x00007fff933fe6a2 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 189
4 AppKit 0x00007fff92660205 -[NSCell _objectValue:forString:errorDescription:] + 159
5 AppKit 0x00007fff9266015f -[NSCell _objectValue:forString:] + 20
6 AppKit 0x00007fff926600db -[NSCell setStringValue:] + 39
7 AppKit 0x00007fff926f43fc -[NSControl setStringValue:] + 138
8 AppKit 0x00007fff928c2ba8 -[NSAlert buildAlertStyle:title:formattedMsg:first:second:third:oldStyle:] + 7479
9 AppKit 0x00007fff928c478b _NXDoLocalRunAlertPanel + 343
10 AppKit 0x00007fff928c461c NSRunAlertPanel + 157
11 Chatty Mac 0x00000001000070bd -[AppDelegate fullDMXReceived:] + 61
12 Chatty Mac 0x0000000100007036 -[AppDelegate receivedData:] + 582
13 Chatty Mac 0x000000010000e751 -[ORSSerialPortDemoController serialPort:didReceiveData:] + 401
14 Chatty Mac 0x000000010000a6fa -[ORSSerialPort receiveData:] + 170
15 Chatty Mac 0x0000000100009676 __block_global_1 + 38
16 libdispatch.dylib 0x00007fff9637bf01 _dispatch_call_block_and_release + 15
17 libdispatch.dylib 0x00007fff963780b6 _dispatch_client_callout + 8
18 libdispatch.dylib 0x00007fff9637d0c8 _dispatch_main_queue_callback_4CF + 275
19 CoreFoundation 0x00007fff93dd70fe __CFRunLoopRun + 1614
20 CoreFoundation 0x00007fff93dd66b2 CFRunLoopRunSpecific + 290
21 HIToolbox 0x00007fff949130a4 RunCurrentEventLoopInMode + 209
22 HIToolbox 0x00007fff94912e42 ReceiveNextEventCommon + 356
23 HIToolbox 0x00007fff94912cd3 BlockUntilNextEventMatchingListInMode + 62
24 AppKit 0x00007fff92682613 _DPSNextEvent + 685
25 AppKit 0x00007fff92681ed2 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 128
26 AppKit 0x00007fff92679283 -[NSApplication run] + 517
27 AppKit 0x00007fff9261dcb6 NSApplicationMain + 869
28 Chatty Mac 0x00000001000056d2 main + 34
29 Chatty Mac 0x0000000100001da4 start + 52
30 ??? 0x0000000000000003 0x0 + 3
)
libc++abi.dylib: terminate called throwing an exception
(lldb)
It's happening where [self fullDMXReceived:buffer]; is called because when I remove it, it doesn't crash.
What's happening here? Can anyone decode this error?
FYI, I'm calling receivedData from another class like this: AppDelegate *theAppDelegate = [[AppDelegate alloc] init]; [theAppDelegate receivedData:string]; Could this be a problem?
When I run AppDelegate *theAppDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
I get this error message:
2012-12-03 06:00:12.791 Chatty Mac[1580:303] An uncaught exception was raised
2012-12-03 06:00:12.793 Chatty Mac[1580:303] -[__NSCFString substringWithRange:]: Range or index out of bounds
2012-12-03 06:00:12.797 Chatty Mac[1580:303] (
0 CoreFoundation 0x00007fff8d0ee0a6 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff887e73f0 objc_exception_throw + 43
2 CoreFoundation 0x00007fff8d0ede7c +[NSException raise:format:] + 204
3 CoreFoundation 0x00007fff8d0c555d -[__NSCFString substringWithRange:] + 109
4 Chatty Mac 0x0000000100006ecb -[AppDelegate receivedData:] + 283
5 Chatty Mac 0x000000010000e772 -[ORSSerialPortDemoController serialPort:didReceiveData:] + 466
6 Chatty Mac 0x000000010000a6ba -[ORSSerialPort receiveData:] + 170
7 Chatty Mac 0x0000000100009636 __block_global_1 + 38
8 libdispatch.dylib 0x00007fff8f634f01 _dispatch_call_block_and_release + 15
9 libdispatch.dylib 0x00007fff8f6310b6 _dispatch_client_callout + 8
10 libdispatch.dylib 0x00007fff8f6360c8 _dispatch_main_queue_callback_4CF + 275
11 CoreFoundation 0x00007fff8d0900fe __CFRunLoopRun + 1614
12 CoreFoundation 0x00007fff8d08f6b2 CFRunLoopRunSpecific + 290
13 HIToolbox 0x00007fff8dbcc0a4 RunCurrentEventLoopInMode + 209
14 HIToolbox 0x00007fff8dbcbe42 ReceiveNextEventCommon + 356
15 HIToolbox 0x00007fff8dbcbcd3 BlockUntilNextEventMatchingListInMode + 62
16 AppKit 0x00007fff8b93b613 _DPSNextEvent + 685
17 AppKit 0x00007fff8b93aed2 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 128
18 AppKit 0x00007fff8b932283 -[NSApplication run] + 517
19 AppKit 0x00007fff8b8d6cb6 NSApplicationMain + 869
20 Chatty Mac 0x0000000100005692 main + 34
21 Chatty Mac 0x0000000100001d64 start + 52
22 ??? 0x0000000000000003 0x0 + 3
)
2012-12-03 06:00:12.799 Chatty Mac[1580:303] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFString substringWithRange:]: Range or index out of bounds'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff8d0ee0a6 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff887e73f0 objc_exception_throw + 43
2 CoreFoundation 0x00007fff8d0ede7c +[NSException raise:format:] + 204
3 CoreFoundation 0x00007fff8d0c555d -[__NSCFString substringWithRange:] + 109
4 Chatty Mac 0x0000000100006ecb -[AppDelegate receivedData:] + 283
5 Chatty Mac 0x000000010000e772 -[ORSSerialPortDemoController serialPort:didReceiveData:] + 466
6 Chatty Mac 0x000000010000a6ba -[ORSSerialPort receiveData:] + 170
7 Chatty Mac 0x0000000100009636 __block_global_1 + 38
8 libdispatch.dylib 0x00007fff8f634f01 _dispatch_call_block_and_release + 15
9 libdispatch.dylib 0x00007fff8f6310b6 _dispatch_client_callout + 8
10 libdispatch.dylib 0x00007fff8f6360c8 _dispatch_main_queue_callback_4CF + 275
11 CoreFoundation 0x00007fff8d0900fe __CFRunLoopRun + 1614
12 CoreFoundation 0x00007fff8d08f6b2 CFRunLoopRunSpecific + 290
13 HIToolbox 0x00007fff8dbcc0a4 RunCurrentEventLoopInMode + 209
14 HIToolbox 0x00007fff8dbcbe42 ReceiveNextEventCommon + 356
15 HIToolbox 0x00007fff8dbcbcd3 BlockUntilNextEventMatchingListInMode + 62
16 AppKit 0x00007fff8b93b613 _DPSNextEvent + 685
17 AppKit 0x00007fff8b93aed2 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 128
18 AppKit 0x00007fff8b932283 -[NSApplication run] + 517
19 AppKit 0x00007fff8b8d6cb6 NSApplicationMain + 869
20 Chatty Mac 0x0000000100005692 main + 34
21 Chatty Mac 0x0000000100001d64 start + 52
22 ??? 0x0000000000000003 0x0 + 3
)
libc++abi.dylib: terminate called throwing an exception
(lldb)
In comments, you say:
AppDelegate *theAppDelegate = [[AppDelegate alloc] init];
[theAppDelegate receivedData:string];
That creates a new AppDelegate object, which means the applicationDidFinishLaunching method isn't being called (it was already called in the first AppDelegate instance). Use the original one instead.
AppDelegate *theAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

Crash With iOS Private API Call

This call:
[UIKeyboardImpl(ShortcutConversionSupport) _shortcutConversionCandidateForInput:]
is crashing my app. Googling and looking through Apple's API documentation brings up no results. I have never seen this call being made anywhere in my app. I also put a break-point at the location I believe it is getting called at. Here is the crash report:
(FYI, the crash log could not be completely symbolicated even when using the correct dSYM file. No idea why)
Last Exception Backtrace:
0 CoreFoundation 0x327e188f __exceptionPreprocess + 163
1 libobjc.A.dylib 0x34837259 objc_exception_throw + 33
2 CoreFoundation 0x327e1789 +[NSException raise:format:] + 1
3 CoreFoundation 0x327e17ab +[NSException raise:format:] + 35
4 CoreFoundation 0x3273bf5b -[__NSCFString substringWithRange:] + 103
5 Buffer 0x000fa061 0xd6000 + 147553
6 UIKit 0x32348137 -[UIKeyboardImpl(ShortcutConversionSupport) _shortcutConversionCandidateForInput:] + 615
7 UIKit 0x32322c07 -[UIKeyboardImpl addInputString:fromVariantKey:] + 287
8 UIKit 0x32322ae1 -[UIKeyboardImpl handleStringInput:fromVariantKey:] + 165
9 UIKit 0x32321829 -[UIKeyboardImpl handleKeyEvent:] + 1501
10 UIKit 0x02b10261 0x2af4000 + 115297
11 UIKit 0x324bb8a3 -[UIKeyboardLayoutStar sendStringAction:forKey:isPopupVariant:] + 487
12 UIKit 0x3231fdcd -[UIKeyboardLayoutStar touchUp:] + 3197
13 UIKit 0x02b2ab47 0x2af4000 + 224071
14 UIKit 0x3231f0fd -[UIKeyboardLayout touchesEnded:withEvent:] + 381
15 UIKit 0x3222292b -[UIWindow _sendTouchesForEvent:] + 319
16 UIKit 0x32222319 -[UIWindow sendEvent:] + 381
17 UIKit 0x32208695 -[UIApplication sendEvent:] + 357
18 UIKit 0x32207f3b _UIApplicationHandleEvent + 5827
19 GraphicsServices 0x3188f22b PurpleEventCallback + 883
20 CoreFoundation 0x327b5523 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 39
21 CoreFoundation 0x327b54c5 __CFRunLoopDoSource1 + 141
22 CoreFoundation 0x327b4313 __CFRunLoopRun + 1371
23 CoreFoundation 0x327374a5 CFRunLoopRunSpecific + 301
24 CoreFoundation 0x3273736d CFRunLoopRunInMode + 105
25 GraphicsServices 0x3188e439 GSEventRunModal + 137
26 UIKit 0x32236cd5 UIApplicationMain + 1081
27 Buffer 0x000d8327 0xd6000 + 8999
28 Buffer 0x000d7dcc 0xd6000 + 7628
I understand that is crashing at substringWithRange: but when does this particular ShortcutConversionSupport method get called? I believe that will help me isolate the issue.
This is the issue:
- (UITextRange *)textRangeFromPosition:(UITextPosition *)fromPosition toPosition:(UITextPosition *)toPosition
{
// Generate IndexedPosition instances that wrap the to and from ranges
IndexedPosition *from = (IndexedPosition *)fromPosition;
IndexedPosition *to = (IndexedPosition *)toPosition;
NSRange range = NSMakeRange(MIN(from.index, to.index), ABS(to.index - from.index));
return [IndexedRange rangeWithNSRange:range];
}
More specifically: ABS(to.index - from.index)
The issue is that the compiler thinks that the product of this negation is a NSUInteger because to.index and from.index are both NSUInteger variables (so the value can never be negative). Therefore, if from.index > to.index the value would overflow producing a value that is the max size of a 32 bit uint rather than a negative value. Also note, ABS uses typeof to determine the return value of the product. This is the fix:
- (UITextRange *)textRangeFromPosition:(UITextPosition *)fromPosition toPosition:(UITextPosition *)toPosition
{
IndexedPosition *from = (IndexedPosition *)fromPosition;
IndexedPosition *to = (IndexedPosition *)toPosition;
NSInteger index = to.index - from.index;
NSRange range = NSMakeRange(MIN(from.index, to.index), ABS(index));
return [IndexedRange rangeWithNSRange:range];
}
By the way, this only occurs when a user has removed all of the Shortcuts in Settings > General > Keyboard > Shortcuts.

App crashes with NSRangeException _PFBatchFaultingArray using NSFetchedResultsController

I receive a lot of crash reports about one specify crash but have no idea where to start to fix it, as I'm not able to reproduce it by myself and crash reports were sent anonymously.
As you'll see it has something to do with the NSFetchedResultsController I am using.
Here is an excerpt from that crash report. The only difference between the reports is the range of the indexes [...]index (someLargeIndex) beyond bounds (1)[...]. The rest stays the same in every single report.
Application Specific Information:
*** Terminating app due to uncaught exception \\\'NSRangeException\\\', reason: \\\'*** -[_PFBatchFaultingArray objectAtIndex:]: index (262144) beyond bounds (1)\\\'
Thread 0 Crashed:
0 libsystem_kernel.dylib 0x31d1e00c __kill + 8
1 libsystem_c.dylib 0x32ac4f95 raise + 17
2 AppName 0x0003d961 uncaught_exception_handler (PLCrashReporter.m:137)
3 CoreFoundation 0x349b57d3 __handleUncaughtException + 239
4 libobjc.A.dylib 0x33f9506b _objc_terminate + 103
5 libstdc++.6.dylib 0x3338ae3d __cxxabiv1::__terminate(void (*)()) + 53
6 libstdc++.6.dylib 0x3338ae91 std::terminate() + 17
7 libstdc++.6.dylib 0x3338af61 __cxa_throw + 85
8 libobjc.A.dylib 0x33f93c8b objc_exception_throw + 71
9 CoreFoundation 0x349b5491 +[NSException raise:format:arguments:] + 69
10 CoreFoundation 0x349b54cb +[NSException raise:format:] + 35
11 CoreData 0x34820fc5 -[_PFBatchFaultingArray objectAtIndex:] + 133
12 CoreData 0x3485e5fb -[_PFMutableProxyArray objectAtIndex:] + 55
13 CoreData 0x348e00f7 +[NSFetchedResultsController(PrivateMethods) _insertIndexForObject:inArray:lowIdx:highIdx:sortDescriptors:] + 99
14 CoreData 0x348e0605 -[NSFetchedResultsController(PrivateMethods) _postprocessInsertedObjects:] + 353
15 CoreData 0x348e0ecf -[NSFetchedResultsController(PrivateMethods) _postprocessUpdatedObjects:] + 507
16 CoreData 0x348e29c7 -[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:] + 1239
17 Foundation 0x35f46183 _nsnote_callback + 143
18 CoreFoundation 0x3498420f __CFXNotificationPost_old + 403
19 CoreFoundation 0x3491eeeb _CFXNotificationPostNotification + 119
20 Foundation 0x35f435d3 -[NSNotificationCenter postNotificationName:object:userInfo:] + 71
21 CoreData 0x34884c07 -[NSManagedObjectContext(_NSInternalNotificationHandling) _postObjectsDidChangeNotificationWithUserInfo:] + 55
22 CoreData 0x34884fcd -[NSManagedObjectContext(_NSInternalChangeProcessing) _createAndPostChangeNotification:withDeletions:withUpdates:withRefreshes:] + 141
23 CoreData 0x34845251 -[NSManagedObjectContext(_NSInternalChangeProcessing) _postRefreshedObjectsNotificationAndClearList] + 77
24 CoreData 0x34844f7f -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] + 1815
25 CoreData 0x348863a5 -[NSManagedObjectContext processPendingChanges] + 17
26 CoreData 0x3482027f _performRunLoopAction + 127
27 CoreFoundation 0x3498ca35 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 17
28 CoreFoundation 0x3498e465 __CFRunLoopDoObservers + 413
29 CoreFoundation 0x3498f75b __CFRunLoopRun + 855
30 CoreFoundation 0x3491fec3 CFRunLoopRunSpecific + 231
31 CoreFoundation 0x3491fdcb CFRunLoopRunInMode + 59
32 GraphicsServices 0x3354641f GSEventRunModal + 115
33 GraphicsServices 0x335464cb GSEventRun + 63
34 UIKit 0x3357dd69 -[UIApplication _run] + 405
35 UIKit 0x3357b807 UIApplicationMain + 671
36 AppName 0x00002b69 main (main.m:15)
I'm sorry, that I'm not able to provide more information. Any suggestions how to start?
This is probably a caching issue with the NSFetchedResultsController. See this question for a little bit more: _PFBatchFaultingArray objectAtIndex:
try looking in you cellForRowAtIndexPath: or wherever you use the results from the NSFetchedResultsController. There, use the following code to see how many results are available to you:
NSArray *sections = fetchController.sections;
int someSection = 0;
id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:someSection];
numberOfObjects = [sectionInfo numberOfObjects];
and then go to the place where you try to get the information, probably where you call:
[fetchedResultsController objectAtIndexPath:indexPath];
and see what you pass over:
NSLog(#"row to be retrieved: %d", indexPath.row);
[fetchedResultsController objectAtIndexPath:indexPath]; //here comes the crash
Eventually, you could check
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
to see how many rows are being returned.