Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

failed to generate newParagraphStyle crash

I have various crashes showing up in my reports that are reporting the following reason:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'failed to generate newParagraphStyle'

The app that this is happening on does set a lot of attributed text programmatically (fonts, colors, sometimes paragraph styles), but I can not find any pattern directly related to this. What I do see is almost all of the reports include a reference in the stack trace to some form of setLineBreakMode:

-[_UICascadingTextStorage(Accessors) setLineBreakMode:] 

I have never been able to recreate this myself, but it does show up at a fairly regular basis on my crash reporting. I am not specifically setting the line break mode in any of these places. Any ideas?

EDIT: I spent more time looking into this and was able to find a pattern to reproduce. So I have a UITextField, and when you hit return on the keyboard the "/n" character is detected and the textfield resigns its first responder status. If at any point after that, you try to modify the text (or attributed text) of the field, it crashes. There is one key element, it only happens if you have typingAttributes set. It does not matter what they are set to.

I tried everything, setting typingAttributes to nil before setting the text, setting a fixed string to make sure it was not what we were setting it to - no luck. Keeps crashing. It seems once you get in this state, the textfield is just broken. A hack to avoid it is to blow away the textfield and create a new one - but its just that. A hack.

Some more interesting info, the way that the code detects the return key was not well written - it detected the "/n" and returned NO in:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

It did not use the appropriate method:

- (BOOL)textFieldShouldReturn:(UITextField *)textField

In one place in the app this actually fixed the crash, in other places it was always using the correct method and the crash is still happening. The only work around is to blow away the textfield and create a new one. To me, this seems like a bug in iOS itself.

like image 459
Stephen Bradley Avatar asked Nov 01 '22 12:11

Stephen Bradley


1 Answers

I had this issue just now and was able to fix it by setting the typingAttributes of the problematic textField to nil directly before resignFirstResponder is called by that same textField.

like image 113
Frankie Avatar answered Nov 13 '22 21:11

Frankie