2013-10-15 1 views
1

sizeWithFont 메서드를 사용하는 응용 프로그램이 있습니다. 이 방법은 SDK 7에서 지원되지 않습니다 아이폰 OS 7에서 응용 프로그램의 출시의 약 5 %에 ​​충돌, 그래서 나는 다음과 같은 카테고리로 교체 :sizeWithFont 및 iOS 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 < 7 풀 또는 캐시에서 글꼴 인스턴스를 반환하므로 하나의 일치하지 않는 release이 할당을 해제하지 않습니다. iOS 7에서는 더 이상 없습니다.