Unexpected crash when typing text into NSTextView

I'm getting the below stacktrace when running myApp and entering some text in a NSTextView, but none of the lines in it correspond to my code. Probably I did something wrong in my code that AppKit should validate with asserts that tell me what I'm doing wrong.


2019-12-10 17:50:54.724652+0100 myApp[85138:2956324] <NSATSTypesetter: 0x600003705380>: Exception NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds raised during typesetting layout manager <NSLayoutManager: 0x103003c40>

1 containers, text backing has 45 characters

selected character range {10, 0} affinity: upstream granularity: character

marked character range {45, 0}

Currently holding 46 glyphs.

Glyph tree contents: 46 characters, 46 glyphs, 3 nodes, 192 node bytes, 128 storage bytes, 320 total bytes, 6.96 bytes per character, 6.96 bytes per glyph

Layout tree contents: 45 characters, 46 glyphs, 1 laid glyphs, 1 laid line fragments, 3 nodes, 192 node bytes, 1712 storage bytes, 1904 total bytes, 42.31 bytes per character, 41.39 bytes per glyph, 1.00 laid glyphs per laid line fragment, 1904.00 bytes per laid line fragment

, glyph range {0 46}. Ignoring...

2019-12-10 17:50:54.724867+0100 myApp[85138:2956324] [General] An uncaught exception was raised

2019-12-10 17:50:54.724930+0100 myApp[85138:2956324] [General] *** -[NSBigMutableString characterAtIndex:]: Index 18446744073709551615 out of bounds; string length 45

2019-12-10 17:50:54.727792+0100 myApp[85138:2956324] [General] (

0 CoreFoundation 0x00007fff2d27df53 __exceptionPreprocess + 250

1 libobjc.A.dylib 0x00007fff63343835 objc_exception_throw + 48

2 Foundation 0x00007fff2f8e213d -[NSBigMutableString characterAtIndex:] + 250

3 UIFoundation 0x00007fff5de0e459 -[NSLayoutManager(NSPrivate) _insertionPointHelperForGlyphAtIndex:] + 1808

4 UIFoundation 0x00007fff5de0da90 _insertionPointHelperForCharacterAtIndex + 1110

5 UIFoundation 0x00007fff5de04fd4 -[NSLayoutManager(NSPrivate) _rectArrayForRange:withinSelectionRange:rangeIsCharRange:singleRectOnly:fullLineRectsOnly:inTextContainer:rectCount:rangeWithinContainer:glyphsDrawOutsideLines:rectArray:rectArrayCapacity:] + 4432

6 UIFoundation 0x00007fff5df56f02 -[NSLayoutManager(NSPrivate) _rectArrayForRange:withinSelectionRange:rangeIsCharRange:singleRectOnly:fullLineRectsOnly:inTextContainer:rectCount:rangeWithinContainer:glyphsDrawOutsideLines:] + 58

7 UIFoundation 0x00007fff5de12344 -[NSLayoutManager rectArrayForCharacterRange:withinSelectedCharacterRange:inTextContainer:rectCount:] + 35

8 AppKit 0x00007fff2a43dd93 -[NSTextView(NSSharing) updateInsertionPointStateAndRestartTimer:] + 1450

9 AppKit 0x00007fff2a4a9400 -[NSTextView(NSSharing) setSelectedRanges:affinity:stillSelecting:] + 1630

10 UIFoundation 0x00007fff5ddb3e66 -[NSLayoutManager textStorage:edited:range:changeInLength:invalidatedRange:] + 397

11 UIFoundation 0x00007fff5dda8ce9 -[NSTextStorage _notifyEdited:range:changeInLength:invalidatedRange:] + 171

12 UIFoundation 0x00007fff5dda440d -[NSTextStorage processEditing] + 373

13 UIFoundation 0x00007fff5ddb3bd1 -[NSTextStorage endEditing] + 134

14 AppKit 0x00007fff2abbeaeb -[NSTextView _insertText:replacementRange:] + 2933

15 AppKit 0x00007fff2a6b8132 -[NSTextInputContext(NSInputContext_WithCompletion) insertText:replacementRange:completionHandler:] + 184

16 AppKit 0x00007fff2ad992d2 __55-[NSTextInputContext handleTSMEvent:completionHandler:]_block_invoke_2.326 + 102

17 AppKit 0x00007fff2a6b8069 -[NSTextInputContext do_HandleTSMEvent_insertFixLenTextLoop:whileCondition:dispatchWorkEach:afterEachInsertText:continuation:] + 105

18 AppKit 0x00007fff2ad97410 -[NSTextInputContext tryHandleTSMEvent_insertFixLenText_withContext:dispatchCondition:setupForDispatch:nestedWorkaroundCondition:nestedWorkaroundDispatchWork:loopCondition:dispatchWorkEach:afterEachInsertText:continuation:] + 310

19 AppKit 0x00007fff2ad98bd4 __55-[NSTextInputContext handleTSMEvent:completionHandler:]_block_invoke.280 + 3027

20 AppKit 0x00007fff2a6b7cec __55-[NSTextInputContext handleTSMEvent:completionHandler:]_block_invoke_2 + 74

21 AppKit 0x00007fff2a6b7c6d -[NSTextInputContext tryHandleTSMEvent_HasMarkedText_withDispatchCondition:dispatchWork:continuation:] + 87

22 AppKit 0x00007fff2a6b7474 -[NSTextInputContext handleTSMEvent:completionHandler:] + 1734

23 AppKit 0x00007fff2a6b6d35 _NSTSMEventHandler + 299

24 HIToolbox 0x00007fff2bd51701 _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 1419

25 HIToolbox 0x00007fff2bd50ad0 _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 338

26 HIToolbox 0x00007fff2bd50973 SendEventToEventTargetWithOptions + 45

27 HIToolbox 0x00007fff2bdb757b SendTSMEvent_WithCompletionHandler + 381

28 HIToolbox 0x00007fff2bdb79d9 __SendUnicodeTextAEToUnicodeDoc_WithCompletionHandler_block_invoke + 387

29 HIToolbox 0x00007fff2bdb7831 __SendFilterTextEvent_WithCompletionHandler_block_invoke + 182

30 HIToolbox 0x00007fff2bdb75ca SendTSMEvent_WithCompletionHandler + 460

31 HIToolbox 0x00007fff2bdb73d1 SendFilterTextEvent_WithCompletionHandler + 225

32 HIToolbox 0x00007fff2bdb70a0 SendUnicodeTextAEToUnicodeDoc_WithCompletionHandler + 277

33 HIToolbox 0x00007fff2bdb6e4d __utDeliverTSMEvent_WithCompletionHandler_block_invoke_2 + 281

34 HIToolbox 0x00007fff2bdb6cad __utDeliverTSMEvent_WithCompletionHandler_block_invoke + 355

35 HIToolbox 0x00007fff2bdb6acc TSMKeyEvent_WithCompletionHandler + 598

36 HIToolbox 0x00007fff2bdb685b __TSMProcessRawKeyEventWithOptionsAndCompletionHandler_block_invoke_4 + 250

37 HIToolbox 0x00007fff2bdb668a __TSMProcessRawKeyEventWithOptionsAndCompletionHandler_block_invoke_3 + 257

38 HIToolbox 0x00007fff2bdb63cf __TSMProcessRawKeyEventWithOptionsAndCompletionHandler_block_invoke_2 + 282

39 HIToolbox 0x00007fff2bdb6133 __TSMProcessRawKeyEventWithOptionsAndCompletionHandler_block_invoke + 274

40 HIToolbox 0x00007fff2bdb5761 TSMProcessRawKeyEventWithOptionsAndCompletionHandler + 3403

41 AppKit 0x00007fff2ad9d8a6 __84-[NSTextInputContext _handleEvent:options:allowingSyntheticEvent:completionHandler:]_block_invoke_3.802 + 110

42 AppKit 0x00007fff2ad9ced9 __204-[NSTextInputContext tryTSMProcessRawKeyEvent_orSubstitution:dispatchCondition:setupForDispatch:furtherCondition:doubleSpaceSubstitutionCondition:doubleSpaceSubstitutionWork:dispatchTSMWork:continuation:]_block_invoke.759 + 115

43 AppKit 0x00007fff2ad9cdca -[NSTextInputContext tryTSMProcessRawKeyEvent_orSubstitution:dispatchCondition:setupForDispatch:furtherCondition:doubleSpaceSubstitutionCondition:doubleSpaceSubstitutionWork:dispatchTSMWork:continuation:] + 245

44 AppKit 0x00007fff2ad9d47e -[NSTextInputContext _handleEvent:options:allowingSyntheticEvent:completionHandler:] + 1261

45 AppKit 0x00007fff2ad9cc89 -[NSTextInputContext _handleEvent:allowingSyntheticEvent:] + 105

46 AppKit 0x00007fff2a6b5b73 -[NSView interpretKeyEvents:] + 209

47 AppKit 0x00007fff2a6b59b6 -[NSTextView keyDown:] + 699

48 AppKit 0x00007fff2a51fdec -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 6512

49 AppKit 0x00007fff2a51e23d -[NSWindow(NSEventRouting) sendEvent:] + 349

50 AppKit 0x00007fff2a3e2daf -[NSApplication(NSEvent) sendEvent:] + 2739

51 AppKit 0x00007fff2a3d1da7 -[NSApplication run] + 707

52 AppKit 0x00007fff2a3c395d NSApplicationMain + 777

53 myApp 0x00000001000c7ead main + 13

54 libdyld.dylib 0x00007fff646a62e5 start + 1

55 ??? 0x0000000000000003 0x0 + 3

)

Replies

Which version of XCode ? of OSX ?

Xcode 11.2.1, macOS 10.15.1.

Could you show the code where you deal with NSTextView.


Do you define a range somewhere ? That would go beyond the limit of 45 chars you seem to have in your container ?

Exception NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds raised during typesetting layout manager <NSLayoutManager: 0x103003c40>

1 containers, text backing has 45 characters

I commented out all the code in the NSTextView subclass so that I was left with only a few segments in the NSDocument subclass that were dealing with the textview, and it turns out that a method containing the line `textView.textStorage.addLayoutManager()` was called twice. Making sure that it is only called once solved the problem. Thanks for your input!

Great. Thanks for feedback. Close the thread so.