UITextfield crash in iOS 13 using textField:shouldChangeCharactersInRange:replacementString

I am frequently getting a crash log related to UITextfield from our live users. It has been reported on only iOS 13.x and not specific to any iPhone device. There is no origin of error mentioned in the stack trace that can help in debugging.


Please refer to stack trace below. textField:shouldChangeCharactersInRange:replacementString is recursively called by our app that leads to crash. It has not been mentioned from where. I have tried to reproduce by different means, but unable to do so. Can anyone draw a guess what may cause this crash?
Thanks.

Crashed: com.apple.main-thread

EXC_BAD_ACCESS KERN_PROTECTION_FAILURE 0x000000016f29fff8


Crashed: com.apple.main-thread
0   libsystem_pthread.dylib        0x1a00657b0 ___chkstk_darwin + 64
1   UIFoundation                   0x1a38fb050 -[NSGlyphGenerator generateGlyphsForGlyphStorage:desiredNumberOfCharacters:glyphIndex:characterIndex:] + 44
2   UIFoundation                   0x1a38fc170 -[NSLayoutManager(NSPrivate) _fillGlyphHoleForCharacterRange:startGlyphIndex:desiredNumberOfCharacters:] + 700
3   UIFoundation                   0x1a393e83c _NSFastFillAllGlyphHolesForCharacterRange + 900
4   UIFoundation                   0x1a38fc6a8 -[NSLayoutManager glyphRangeForCharacterRange:actualCharacterRange:] + 56
5   UIFoundation                   0x1a38ead38 -[NSLayoutManager(NSPrivate) _fillLayoutHoleForCharacterRange:desiredNumberOfLines:isSoft:] + 700
6   UIFoundation                   0x1a38ef7b4 -[NSLayoutManager(NSPrivate) _fillLayoutHoleAtIndex:desiredNumberOfLines:] + 196
7   UIFoundation                   0x1a38f7240 -[NSLayoutManager(NSPrivate) _markSelfAsDirtyForBackgroundLayout:] + 424
8   UIFoundation                   0x1a38f1dd8 -[NSLayoutManager(NSPrivate) _invalidateLayoutForExtendedCharacterRange:isSoft:invalidateUsage:] + 2332
9   UIFoundation                   0x1a38f206c -[NSLayoutManager textStorage:edited:range:changeInLength:invalidatedRange:] + 212
10  UIFoundation                   0x1a38f1f18 -[NSTextStorage _notifyEdited:range:changeInLength:invalidatedRange:] + 184
11  UIFoundation                   0x1a38ef824 -[NSTextStorage endEditing] + 92
12  UIFoundation                   0x1a3903a0c -[NSTextStorage coordinateEditing:] + 68
13  UIKitCore                      0x1a468541c -[UIFieldEditor _setAttributedTextInRange:replacementText:andSetCaretSelectionAfterText:] + 676
14  UIKitCore                      0x1a4684e1c -[UIFieldEditor _setTextInRange:replacementText:andSetCaretSelectionAfterText:] + 204
15  UIKitCore                      0x1a4697434 -[UITextField setText:] + 436
16  MyApp-QA                   0x100a9e53c -[TextField textField:shouldChangeCharactersInRange:replacementString:] + 311 (TextField.m:311)
17  UIKitCore                      0x1a46a3128 -[UITextField _delegateShouldChangeCharactersInTextStorageRange:replacementString:delegateCares:] + 160
18  UIKitCore                      0x1a469cee8 -[UITextField keyboardInput:shouldReplaceTextInRange:replacementText:] + 92
19  UIKitCore                      0x1a41fc56c -[UIKeyboardImpl acceptAutocorrection:executionContextPassingTIKeyboardCandidate:] + 536
20  UIKitCore                      0x1a41ff6d4 -[UIKeyboardImpl acceptPredictiveInput:executionContext:] + 664
21  UIKitCore                      0x1a41f5024 -[UIKeyboardImpl inputManagerDidGenerateAutocorrections:executionContext:] + 668
22  UIKitCore                      0x1a4221d88 -[UIKeyboardTaskEntry execute:] + 180
23  UIKitCore                      0x1a42207b8 -[UIKeyboardTaskQueue continueExecutionOnMainThread] + 316
24  UIKitCore                      0x1a4220b20 -[UIKeyboardTaskQueue performTaskOnMainThread:waitUntilDone:] + 320
25  UIKitCore                      0x1a41de6a4 -[UIKeyboardImpl generateAutofillCandidateByAddingTask:] + 156
26  UIKitCore                      0x1a41f1d18 -[UIKeyboardImpl selectionDidChange:] + 340
27  UIKitCore                      0x1a46af9fc __52-[UITextInputController _coordinateSelectionChange:]_block_invoke + 504
28  UIFoundation                   0x1a38f4b64 -[NSTextStorage coordinateReading:] + 56
29  UIKitCore                      0x1a46af7dc -[UITextInputController _coordinateSelectionChange:] + 148
30  UIKitCore                      0x1a46af364 -[UITextInputController _setSelectedTextRange:] + 452
31  UIKitCore                      0x1a46af190 -[UITextInputController setSelectedRange:] + 72
32  UIKitCore                      0x1a4685498 -[UIFieldEditor _setAttributedTextInRange:replacementText:andSetCaretSelectionAfterText:] + 800
33  UIKitCore                      0x1a4684e1c -[UIFieldEditor _setTextInRange:replacementText:andSetCaretSelectionAfterText:] + 204
34  UIKitCore                      0x1a4697434 -[UITextField setText:] + 436
35  MyApp-QA                   0x100a9e53c -[TextField textField:shouldChangeCharactersInRange:replacementString:] + 311 (TextField.m:311)
36  UIKitCore                      0x1a46a3128 -[UITextField _delegateShouldChangeCharactersInTextStorageRange:replacementString:delegateCares:] + 160
37  UIKitCore                      0x1a469cee8 -[UITextField keyboardInput:shouldReplaceTextInRange:replacementText:] + 92
38  UIKitCore                      0x1a41fc56c -[UIKeyboardImpl acceptAutocorrection:executionContextPassingTIKeyboardCandidate:] + 536
39  UIKitCore                      0x1a41ff6d4 -[UIKeyboardImpl acceptPredictiveInput:executionContext:] + 664
40  UIKitCore                      0x1a41f5024 -[UIKeyboardImpl inputManagerDidGenerateAutocorrections:executionContext:] + 668
41  UIKitCore                      0x1a4221d88 -[UIKeyboardTaskEntry execute:] + 180
42  UIKitCore                      0x1a42207b8 -[UIKeyboardTaskQueue continueExecutionOnMainThread] + 316
43  UIKitCore                      0x1a4220b20 -[UIKeyboardTaskQueue performTaskOnMainThread:waitUntilDone:] + 320
44  UIKitCore                      0x1a41de6a4 -[UIKeyboardImpl generateAutofillCandidateByAddingTask:] + 156
45  UIKitCore                      0x1a41f1d18 -[UIKeyboardImpl selectionDidChange:] + 340
46  UIKitCore                      0x1a46af9fc __52-[UITextInputController _coordinateSelectionChange:]_block_invoke + 504
47  UIFoundation                   0x1a38f4b64 -[NSTextStorage coordinateReading:] + 56
48  UIKitCore                      0x1a46af7dc -[UITextInputController _coordinateSelectionChange:] + 148
49  UIKitCore                      0x1a46af364 -[UITextInputController _setSelectedTextRange:] + 452
50  UIKitCore                      0x1a46af190 -[UITextInputController setSelectedRange:] + 72
51  UIKitCore                      0x1a4685498 -[UIFieldEditor _setAttributedTextInRange:replacementText:andSetCaretSelectionAfterText:] + 800
52  UIKitCore                      0x1a4684e1c -[UIFieldEditor _setTextInRange:replacementText:andSetCaretSelectionAfterText:] + 204
53  UIKitCore                      0x1a4697434 -[UITextField setText:] + 436
54  MyApp-QA                   0x100a9e53c -[TextField textField:shouldChangeCharactersInRange:replacementString:] + 311 (TextField.m:311)
55  UIKitCore                      0x1a46a3128 -[UITextField _delegateShouldChangeCharactersInTextStorageRange:replacementString:delegateCares:] + 160
56  UIKitCore                      0x1a469cee8 -[UITextField keyboardInput:shouldReplaceTextInRange:replacementText:] + 92
57  UIKitCore                      0x1a41fc56c -[UIKeyboardImpl acceptAutocorrection:executionContextPassingTIKeyboardCandidate:] + 536
58  UIKitCore                      0x1a41ff6d4 -[UIKeyboardImpl acceptPredictiveInput:executionContext:] + 664
59  UIKitCore                      0x1a41f5024 -[UIKeyboardImpl inputManagerDidGenerateAutocorrections:executionContext:] + 668
60  UIKitCore                      0x1a4221d88 -[UIKeyboardTaskEntry execute:] + 180
61  UIKitCore                      0x1a42207b8 -[UIKeyboardTaskQueue continueExecutionOnMainThread] + 316
62  UIKitCore                      0x1a4220b20 -[UIKeyboardTaskQueue performTaskOnMainThread:waitUntilDone:] + 320
63  UIKitCore                      0x1a41de6a4 -[UIKeyboardImpl generateAutofillCandidateByAddingTask:] + 156
64  UIKitCore                      0x1a41f1d18 -[UIKeyboardImpl selectionDidChange:] + 340
65  UIKitCore                      0x1a46af9fc __52-[UITextInputController _coordinateSelectionChange:]_block_invoke + 504
66  UIFoundation                   0x1a38f4b64 -[NSTextStorage coordinateReading:] + 56
67  UIKitCore                      0x1a46af7dc -[UITextInputController _coordinateSelectionChange:] + 148
68  UIKitCore                      0x1a46af364 -[UITextInputController _setSelectedTextRange:] + 452
69  UIKitCore                      0x1a46af190 -[UITextInputController setSelectedRange:] + 72
70  UIKitCore                      0x1a4685498 -[UIFieldEditor _setAttributedTextInRange:replacementText:andSetCaretSelectionAfterText:] + 800
71  UIKitCore                      0x1a4684e1c -[UIFieldEditor _setTextInRange:replacementText:andSetCaretSelectionAfterText:] + 204
72  UIKitCore                      0x1a4697434 -[UITextField setText:] + 436
73  MyApp-QA                   0x100a9e53c -[TextField textField:shouldChangeCharactersInRange:replacementString:] + 311 (TextField.m:311)
74  UIKitCore                      0x1a46a3128 -[UITextField _delegateShouldChangeCharactersInTextStorageRange:replacementString:delegateCares:] + 160
75  UIKitCore                      0x1a469cee8 -[UITextField keyboardInput:shouldReplaceTextInRange:replacementText:] + 92
76  UIKitCore                      0x1a41fc56c -[UIKeyboardImpl acceptAutocorrection:executionContextPassingTIKeyboardCandidate:] + 536
77  UIKitCore                      0x1a41ff6d4 -[UIKeyboardImpl acceptPredictiveInput:executionContext:] + 664
78  UIKitCore                      0x1a41f5024 -[UIKeyboardImpl inputManagerDidGenerateAutocorrections:executionContext:] + 668
79  UIKitCore                      0x1a4221d88 -[UIKeyboardTaskEntry execute:] + 180
80  UIKitCore                      0x1a42207b8 -[UIKeyboardTaskQueue continueExecutionOnMainThread] + 316
81  UIKitCore                      0x1a4220b20 -[UIKeyboardTaskQueue performTaskOnMainThread:waitUntilDone:] + 320
82  UIKitCore                      0x1a41de6a4 -[UIKeyboardImpl generateAutofillCandidateByAddingTask:] + 156
83  UIKitCore                      0x1a41f1d18 -[UIKeyboardImpl selectionDidChange:] + 340
84  UIKitCore                      0x1a46af9fc __52-[UITextInputController _coordinateSelectionChange:]_block_invoke + 504
85  UIFoundation                   0x1a38f4b64 -[NSTextStorage coordinateReading:] + 56
86  UIKitCore                      0x1a46af7dc -[UITextInputController _coordinateSelectionChange:] + 148
87  UIKitCore                      0x1a46af364 -[UITextInputController _setSelectedTextRange:] + 452
88  UIKitCore                      0x1a46af190 -[UITextInputController setSelectedRange:] + 72
89  UIKitCore                      0x1a4685498 -[UIFieldEditor _setAttributedTextInRange:replacementText:andSetCaretSelectionAfterText:] + 800
90  UIKitCore                      0x1a4684e1c -[UIFieldEditor _setTextInRange:replacementText:andSetCaretSelectionAfterText:] + 204
91  UIKitCore                      0x1a4697434 -[UITextField setText:] + 436
92  MyApp-QA                   0x100a9e53c -[TextField textField:shouldChangeCharactersInRange:replacementString:] + 311 (TextField.m:311)
93  UIKitCore                      0x1a46a3128 -[UITextField _delegateShouldChangeCharactersInTextStorageRange:replacementString:delegateCares:] + 160
94  UIKitCore                      0x1a469cee8 -[UITextField keyboardInput:shouldReplaceTextInRange:replacementText:] + 92
95  UIKitCore                      0x1a41fc56c -[UIKeyboardImpl acceptAutocorrection:executionContextPassingTIKeyboardCandidate:] + 536
96  UIKitCore                      0x1a41ff6d4 -[UIKeyboardImpl acceptPredictiveInput:executionContext:] + 664
97  UIKitCore                      0x1a41f5024 -[UIKeyboardImpl inputManagerDidGenerateAutocorrections:executionContext:] + 668
98  UIKitCore                      0x1a4221d88 -[UIKeyboardTaskEntry execute:] + 180
99  UIKitCore                      0x1a42207b8 -[UIKeyboardTaskQueue continueExecutionOnMainThread] + 316
100 UIKitCore                      0x1a4220b20 -[UIKeyboardTaskQueue performTaskOnMainThread:waitUntilDone:] + 320
101 UIKitCore                      0x1a41de6a4 -[UIKeyboardImpl generateAutofillCandidateByAddingTask:] + 156
102 UIKitCore                      0x1a41f1d18 -[UIKeyboardImpl selectionDidChange:] + 340
103 UIKitCore                      0x1a46af9fc __52-[UITextInputController _coordinateSelectionChange:]_block_invoke + 504
104 UIFoundation                   0x1a38f4b64 -[NSTextStorage coordinateReading:] + 56
105 UIKitCore                      0x1a46af7dc -[UITextInputController _coordinateSelectionChange:] + 148
106 UIKitCore                      0x1a46af364 -[UITextInputController _setSelectedTextRange:] + 452
107 UIKitCore                      0x1a46af190 -[UITextInputController setSelectedRange:] + 72
108 UIKitCore                      0x1a4685498 -[UIFieldEditor _setAttributedTextInRange:replacementText:andSetCaretSelectionAfterText:] + 800
109 UIKitCore                      0x1a4684e1c -[UIFieldEditor _setTextInRange:replacementText:andSetCaretSelectionAfterText:] + 204
110 UIKitCore                      0x1a4697434 -[UITextField setText:] + 436
111 MyApp-QA                   0x100a9e53c -[TextField textField:shouldChangeCharactersInRange:replacementString:] + 311 (TextField.m:311)
112 UIKitCore                      0x1a46a3128 -[UITextField _delegateShouldChangeCharactersInTextStorageRange:replacementString:delegateCares:] + 160
113 UIKitCore                      0x1a469cee8 -[UITextField keyboardInput:shouldReplaceTextInRange:replacementText:] + 92
114 UIKitCore                      0x1a41fc56c -[UIKeyboardImpl acceptAutocorrection:executionContextPassingTIKeyboardCandidate:] + 536
115 UIKitCore                      0x1a41ff6d4 -[UIKeyboardImpl acceptPredictiveInput:executionContext:] + 664
116 UIKitCore                      0x1a41f5024 -[UIKeyboardImpl inputManagerDidGenerateAutocorrections:executionContext:] + 668
117 UIKitCore                      0x1a4221d88 -[UIKeyboardTaskEntry execute:] + 180
118 UIKitCore                      0x1a42207b8 -[UIKeyboardTaskQueue continueExecutionOnMainThread] + 316
119 UIKitCore                      0x1a4220b20 -[UIKeyboardTaskQueue performTaskOnMainThread:waitUntilDone:] + 320
120 UIKitCore                      0x1a41de6a4 -[UIKeyboardImpl generateAutofillCandidateByAddingTask:] + 156
121 UIKitCore                      0x1a41f1d18 -[UIKeyboardImpl selectionDidChange:] + 340
122 UIKitCore                      0x1a46af9fc __52-[UITextInputController _coordinateSelectionChange:]_block_invoke + 504
123 UIFoundation                   0x1a38f4b64 -[NSTextStorage coordinateReading:] + 56
124 UIKitCore                      0x1a46af7dc -[UITextInputController _coordinateSelectionChange:] + 148
125 UIKitCore                      0x1a46af364 -[UITextInputController _setSelectedTextRange:] + 452
126 UIKitCore                      0x1a46af190 -[UITextInputController setSelectedRange:] + 72
127 UIKitCore                      0x1a4685498 -[UIFieldEditor _setAttributedTextInRange:replacementText:andSetCaretSelectionAfterText:] + 800
128 UIKitCore                      0x1a4684e1c -[UIFieldEditor _setTextInRange:replacementText:andSetCaretSelectionAfterText:] + 204
129 UIKitCore                      0x1a4697434 -[UITextField setText:] + 436
130 MyApp-QA                   0x100a9e53c -[TextField textField:shouldChangeCharactersInRange:replacementString:] + 311 (TextField.m:311)
131 UIKitCore                      0x1a46a3128 -[UITextField _delegateShouldChangeCharactersInTextStorageRange:replacementString:delegateCares:] + 160
132 UIKitCore                      0x1a469cee8 -[UITextField keyboardInput:shouldReplaceTextInRange:replacementText:] + 92
133 UIKitCore                      0x1a41fc56c -[UIKeyboardImpl acceptAutocorrection:executionContextPassingTIKeyboardCandidate:] + 536
134 UIKitCore                      0x1a41ff6d4 -[UIKeyboardImpl acceptPredictiveInput:executionContext:] + 664
135 UIKitCore                      0x1a41f5024 -[UIKeyboardImpl inputManagerDidGenerateAutocorrections:executionContext:] + 668
136 UIKitCore                      0x1a4221d88 -[UIKeyboardTaskEntry execute:] + 180
137 UIKitCore                      0x1a42207b8 -[UIKeyboardTaskQueue continueExecutionOnMainThread] + 316
138 UIKitCore                      0x1a4220b20 -[UIKeyboardTaskQueue performTaskOnMainThread:waitUntilDone:] + 320
139 UIKitCore                      0x1a41de6a4 -[UIKeyboardImpl generateAutofillCandidateByAddingTask:] + 156
140 UIKitCore                      0x1a41f1d18 -[UIKeyboardImpl selectionDidChange:] + 340
141 UIKitCore                      0x1a46af9fc __52-[UITextInputController _coordinateSelectionChange:]_block_invoke + 504
142 UIFoundation                   0x1a38f4b64 -[NSTextStorage coordinateReading:] + 56
143 UIKitCore                      0x1a46af7dc -[UITextInputController _coordinateSelectionChange:] + 148
144 UIKitCore                      0x1a46af364 -[UITextInputController _setSelectedTextRange:] + 452
145 UIKitCore                      0x1a46af190 -[UITextInputController setSelectedRange:] + 72
146 UIKitCore                      0x1a4685498 -[UIFieldEditor _setAttributedTextInRange:replacementText:andSetCaretSelectionAfterText:] + 800
147 UIKitCore                      0x1a4684e1c -[UIFieldEditor _setTextInRange:replacementText:andSetCaretSelectionAfterText:] + 204
148 UIKitCore                      0x1a4697434 -[UITextField setText:] + 436
149 MyApp-QA                   0x100a9e53c -[TextField textField:shouldChangeCharactersInRange:replacementString:] + 311 (TextField.m:311)
150 UIKitCore                      0x1a46a3128 -[UITextField _delegateShouldChangeCharactersInTextStorageRange:replacementString:delegateCares:] + 160
151 UIKitCore                      0x1a469cee8 -[UITextField keyboardInput:shouldReplaceTextInRange:replacementText:] + 92
152 UIKitCore                      0x1a41fc56c -[UIKeyboardImpl acceptAutocorrection:executionContextPassingTIKeyboardCandidate:] + 536
153 UIKitCore                      0x1a41ff6d4 -[UIKeyboardImpl acceptPredictiveInput:executionContext:] + 664
154 UIKitCore                      0x1a41f5024 -[UIKeyboardImpl inputManagerDidGenerateAutocorrections:executionContext:] + 668
155 UIKitCore                      0x1a4221d88 -[UIKeyboardTaskEntry execute:] + 180
156 UIKitCore                      0x1a42207b8 -[UIKeyboardTaskQueue continueExecutionOnMainThread] + 316
157 UIKitCore                      0x1a4220b20 -[UIKeyboardTaskQueue performTaskOnMainThread:waitUntilDone:] + 320
158 UIKitCore                      0x1a41de6a4 -[UIKeyboardImpl generateAutofillCandidateByAddingTask:] + 156
159 UIKitCore                      0x1a41f1d18 -[UIKeyboardImpl selectionDidChange:] + 340
160 UIKitCore                      0x1a46af9fc __52-[UITextInputController _coordinateSelectionChange:]_block_invoke + 504

Seems you crash at the same place in shouldChangeCharactersInRange (+ 311).


Could you first show the complete func shouldChangeCharactersInRange ?

Is there anything specific you would do with the keyboard ?


Note:

Is it swift code or objc (what I guess) ?

Thank you @Claude31 for quick response. I am not on my office system currently, so I cannot show you code. What I can tell you is that on line 311, cursor position is set to using

selectedTextRange
property. Code is written in Objective C. Can it somehow call shouldChangeCharactersInRange again? I am unable to reproduce crash.

As soon as you can send code, please show.


Note that 311 is not a line count, it is the instruction pointer. So probably about line 10 of the func.

Crashlytics has pointed out line 311, that's why I mentioned. I have checked other crashes as well, and I can confirm line number mentioned points out to the correct LOC producing crash.


MyTextField.m - Line 311

-[MyTextField textField:shouldChangeCharactersInRange:replacementString:] + 311


Well here is the code for function:


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
     
    NSString *updatedString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    if (![StringUtils isEmptyOrNull:string] && _maxLength && updatedString.length > _maxLength)
        return NO;
 
    UITextPosition *initialCursorPostition = [self getInitialPostitionOfCursorWithRange:range andString:string];
    textField.text = updatedString;
    [self moveCursorToPosition:initialCursorPostition]; //Line 311 in the code
    [self setHiddenClearButtonWithText:updatedString];
 
    if (_textFieldDelegate && [_textFieldDelegate respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)]) {
        return [_textFieldDelegate textField:textField shouldChangeCharactersInRange:range replacementString:string];
    }
    return NO;
}

-(void)moveCursorToPosition:(UITextPosition*)position {
    if(position)
        [self setSelectedTextRange:[self textRangeFromPosition:position toPosition:position]];
}

-(UITextPosition*)getInitialPostitionOfCursorWithRange:(NSRange)range andString:(NSString*)string {
    UITextPosition *beginning = self.beginningOfDocument;
    UITextPosition *cursorLocation = [self positionFromPosition:beginning offset:(range.location + string.length)];
    return cursorLocation;
}

I cannot find any reference to getInitialPostitionOfCursorWithRange in documentation.


Where is this func defined ?

Sorry, my bad. I forgot to add the chunk. Please find it in the previous reply.

Not sure of this, but wonder if that could be the cause:


    textField.text = updatedString;
    [self moveCursorToPosition:initialCursorPostition]; //Line 311 in the code

you change the textField

then you moveCursor (relative to textField.text)


I wonder if this does not change some var in memory, causing the crash, unless this is due to setSelectedTextRange.

Note: could not find func setSelectedTextRange

Do you have the solution?
did you resolved this issue?
Did you find a solution for this? I'm getting the very same error.
if you mutate the state of the text on a UITextField (Like [textField setText:text]) during swipe entry , it'll make shouldChangeCharactersInRange called repeatedly and your app goes into an infinite loop (even if you return false in func)
UITextfield crash in iOS 13 using textField:shouldChangeCharactersInRange:replacementString
 
 
Q