2013-10-15 68 views
1

我有使用sizeWithFont方法的应用程序。它崩溃的方法是在SDK 7弃用在iOS 7的应用程序的启动的5%左右,所以我有以下类别替代它:iOS上的sizeWithFont崩溃和替换7

#import "NSString+mySizeWithFont.h" 
#import "Constants.h" 

@implementation NSString (mySizeWithFont) 

- (CGSize)mySizeWithFont:(UIFont *)font { 
    if (is_iOS7) { 
     CGSize size = CGSizeMake(MAXFLOAT, MAXFLOAT); 
     return [self mySizeWithFont:font constrainedToSize:size]; 
    } else { 
     return [self sizeWithFont:font]; 
    } 
} 

- (CGSize)mySizeWithFont:(UIFont*)font constrainedToSize:(CGSize)size { 
    if (is_iOS7) { 
     NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil]; 
     CGRect frame = [self boundingRectWithSize:size options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) attributes:attributesDictionary context:nil]; 
     return frame.size; 
    } else { 
     return [self sizeWithFont:font constrainedToSize:size]; 
    } 
} 

@end 

但现在我有相同的5%另一个崩溃的发射。有2种类型的崩溃报告:

Exception Type: EXC_BAD_ACCESS (SIGSEGV) 
Exception Subtype: KERN_INVALID_ADDRESS at 0x0005006f 
Triggered by Thread: 0 

Thread 0 Crashed: 
0 libobjc.A.dylib     0x3902cb76 objc_msgSend + 22 
1 CoreFoundation     0x2ec74c56 +[__NSDictionaryI __new:::::] + 358 
2 CoreFoundation     0x2ec749c6 -[__NSPlaceholderDictionary initWithObjects:forKeys:count:] + 238 
3 CoreFoundation     0x2ec794d4 +[NSDictionary dictionaryWithObjectsAndKeys:] + 372 
4 *** MYAPP ***     0x000cd99e -[NSString(mySizeWithFont) mySizeWithFont:constrainedToSize:] (NSString+mySizeWithFont.m:25) 
5 *** MYAPP ***     0x000cd7e6 -[NSString(mySizeWithFont) mySizeWithFont:] (NSString+mySizeWithFont.m:17) 
6 *** MYAPP ***     0x000d36ae -[LiteVersionHomepageLink drawRect:] (LiteVersionHomepageLink.m:43) 

这一个:

Exception Type: EXC_CRASH (SIGABRT) 
Exception Codes: 0x0000000000000000, 0x0000000000000000 
Triggered by Thread: 0 

Last Exception Backtrace: 
0 CoreFoundation     0x2ed36e86 __exceptionPreprocess + 126 
1 libobjc.A.dylib     0x390316c2 objc_exception_throw + 34 
2 CoreFoundation     0x2ed3a7b2 -[NSObject(NSObject) doesNotRecognizeSelector:] + 198 
3 CoreFoundation     0x2ed390b2 ___forwarding___ + 702 
4 CoreFoundation     0x2ec87e94 __forwarding_prep_0___ + 20 
5 UIFoundation     0x3660790e __NSStringDrawingEngine + 2950 
6 UIFoundation     0x36606d5a -[NSString(NSExtendedStringDrawing) boundingRectWithSize:options:attributes:context:] + 130 
7 *** MYAPP ***     0x000e89d4 -[NSString(mySizeWithFont) mySizeWithFont:constrainedToSize:] (NSString+mySizeWithFont.m:26) 
8 *** MYAPP ***     0x000e87e6 -[NSString(mySizeWithFont) mySizeWithFont:] (NSString+mySizeWithFont.m:17) 
9 *** MYAPP ***     0x000ee6ae -[LiteVersionHomepageLink drawRect:] (LiteVersionHomepageLink.m:43) 

LiteVersionHomepageLink从UIView的继承:从下列方法

@interface LiteVersionHomepageLink : UIView { 
    NSString *text; 
    UIFont *textFont; 
} 

而且mySizeWithFont被称为:

- (void)drawRect:(CGRect)rect { 
    [[UIImage imageNamed:@"[email protected]"] drawInRect:rect]; 
    if (text && textFont) { 
     [[UIColor whiteColor] set]; 
     float height = [text mySizeWithFont:textFont].height/2; 
     [text drawInRect:CGRectMake(0, rect.size.height/2 - height, rect.size.width, rect.size.height/2 + height) withFont:textFont lineBreakMode:NSLineBreakByCharWrapping alignment:NSTextAlignmentCenter]; 
    } 
} 

怎么了?如何解决崩溃?非常感谢您的帮助!

+0

您仍然可以在iOS 7下使用'sizeWithFont:'。由于您的Base SDK不是iOS 7,您甚至不会得到弃用警告。 – rmaddy

+0

我正在使用带有部署目标5的SDK 7.但问题是崩溃 - 在已弃用的版本和替换版本上。无论如何,我有5%的应用程序启动崩溃。 – Dmitry

+1

摆脱您的'is_iOS7'检查,并用适当的检查替换'boundingRectWithSize ...'方法的存在。 – rmaddy

回答

2

它在我看来像你打电话给你的例程mySizeWithFont与一个无效的字体。要么是已经发布的,要么是从未分配过的。我会在你的字体分配附近放置NSLog面包屑,并且在面包屑之前确保你的字体先被分配。如果是这样,可能是ARC在你使用它之前抛弃了你的字体 - 可能需要@property(strong)或类似的东西。

+0

但是我可以看到与95%的时间相同的步骤崩溃。 – Dmitry

+1

它可能是ARC,它在实际释放你的记忆时是不确定的。我有时会打赌,在它用到这里之前它会抛弃你的字体。尝试在它使用的对象中创建UIFont对象 - 这应该清除你的麻烦。 –

+0

我已经添加NSZombies,并没有看到任何与textFont错误。为什么?我不使用ARC。 – Dmitry

2

我的两分钱:在我的应用程序中,我有一个UIFont对象的过度发布错误。我会调用[UIFont systemFontOfSize:]并将结果存储在一个文件静态变量中,而不保留。

直到iOS 7,我会摆脱它,然后在[sizeWithFont]崩溃开始即将到来。看起来像iOS <将从池或缓存返回字体实例,以便单个不匹配的release不会释放它们。在iOS 7中 - 现在不行了。