hsusmita / ResponsiveLabel

MIT License
218 stars 36 forks source link

The new update crashing in iPhone 5, 5S and 5C? #41

Open kidsid49 opened 8 years ago

kidsid49 commented 8 years ago

Your inclusion of new category in latest commits keeps crashing on iPhone 5 family device. any clue?

line 29 -[NSMutableAttributedString(BoundChecker) removeAttributeWithBoundsCheck:range:](exc_bad_access kern_invalid_address) That's why what my crashanalytics showing me.

Crashed: com.apple.main-thread

0  libobjc.A.dylib                0x23d23a82 objc_msgSend + 1
1  Foundation                     0x24cb6b71 -[NSMutableRLEArray replaceObjectsInRange:withObject:length:] + 384
2  Foundation                     0x24cb730d -[NSConcreteMutableAttributedString setAttributes:range:] + 92
3  Foundation                     0x24cc55f3 -[NSMutableAttributedString removeAttribute:range:] + 182
4  UIFoundation                   0x28ac52a3 __47-[NSConcreteTextStorage removeAttribute:range:]_block_invoke + 174
5  UIFoundation                   0x28ac5119 -[NSConcreteTextStorage removeAttribute:range:] + 172
6  ResponsiveLabel                0xc69b57 -[NSMutableAttributedString(BoundChecker) removeAttributeWithBoundsCheck:range:] (NSMutableAttributedString+BoundChecker.m:29)
7  ResponsiveLabel                0xc6be2b __51-[ResponsiveLabel removeAttributeForTruncatedRange]_block_invoke (ResponsiveLabel.m:403)
8  CoreFoundation                 0x244c3cb5 __65-[__NSDictionaryI enumerateKeysAndObjectsWithOptions:usingBlock:]_block_invoke + 56
9  CoreFoundation                 0x244b49e9 -[__NSDictionaryI enumerateKeysAndObjectsWithOptions:usingBlock:] + 164
10 ResponsiveLabel                0xc6bd23 -[ResponsiveLabel removeAttributeForTruncatedRange] (ResponsiveLabel.m:405)
11 ResponsiveLabel                0xc6bb8f -[ResponsiveLabel updateTextStorageReplacingRange:] (ResponsiveLabel.m:396)
12 ResponsiveLabel                0xc6ba35 -[ResponsiveLabel appendTokenIfNeeded] (ResponsiveLabel.m:375)
13 ResponsiveLabel                0xc6b0c9 -[ResponsiveLabel drawTextInRect:] (ResponsiveLabel.m:251)
14 UIKit                          0x28bbff4d -[UILabel drawRect:] + 88
15 UIKit                          0x28bbfecb -[UIView(CALayerDelegate) drawLayer:inContext:] + 386
16 QuartzCore                     0x26bd3325 -[CALayer drawInContext:] + 228
17 QuartzCore                     0x26bbcb23 CABackingStoreUpdate_ + 1966
18 QuartzCore                     0x26cb179d ___ZN2CA5Layer8display_Ev_block_invoke + 56
19 QuartzCore                     0x26bbbfdb CA::Layer::display_() + 1322
20 QuartzCore                     0x26b9ff0d CA::Layer::display_if_needed(CA::Transaction*) + 208
21 QuartzCore                     0x26b9fbc5 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 24
22 QuartzCore                     0x26b9f081 CA::Context::commit_transaction(CA::Transaction*) + 368
23 QuartzCore                     0x26b9ed55 CA::Transaction::commit() + 520
24 QuartzCore                     0x26b984ff CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 138
25 CoreFoundation                 0x2451b2b1 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 20
26 CoreFoundation                 0x245195a7 __CFRunLoopDoObservers + 282
27 CoreFoundation                 0x245199e5 __CFRunLoopRun + 972
28 CoreFoundation                 0x244681c9 CFRunLoopRunSpecific + 516
29 CoreFoundation                 0x24467fbd CFRunLoopRunInMode + 108
30 GraphicsServices               0x25a84af9 GSEventRunModal + 160
31 UIKit                          0x28ba0435 UIApplicationMain + 144
32 Goodshows                      0xf15c1 main (main.m:14)
33 libdispatch.dylib              0x24114873 (Missing)
uwjimmyxu commented 8 years ago

I'm getting the same crash with iPhone 6, 5, and iPod touch 6g, so I'm assuming it affects all devices.

It looks like it's caused by the range parameter of removeAttributeWithBoundsCheck having a negative (very high positive integer) length. Tracing the thread up, I think it's related to truncation.

These are my values in removeAttributeForTruncatedRange:

NSRange availableRange = NSMakeRange(self.truncatedPatternRange.location, self.textStorage.length - self.attributedTruncationToken.length - self.truncatedPatternRange.location);

(NSRange) availableRange = location=75, length=4294967292 self.textStorage.length = 77 self.attributedTruncationToken.length = 6 self.truncatedPatternRange.location = 75

This change fixed the issue for me:

- (void)removeAttributeForTruncatedRange {
NSDictionary *patternAttributes = [self.rangeAttributeDictionary objectForKey:[NSValue valueWithRange:self.truncatedPatternRange]];
[patternAttributes enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    NSInteger truncatedPatternRangeLocation = self.truncatedPatternRange.location;
    if(truncatedPatternRangeLocation + self.attributedTruncationToken.length > self.textStorage.length) {
        truncatedPatternRangeLocation = self.textStorage.length - self.attributedTruncationToken.length;
    }

    NSRange availableRange = NSMakeRange(truncatedPatternRangeLocation, self.textStorage.length - self.attributedTruncationToken.length - truncatedPatternRangeLocation);
    [self.textStorage removeAttributeWithBoundsCheck:key range:availableRange];
}];

}`