2010-07-14 2 views
4

내보기로 UILabel의를 추가하는 기능을 가지고 : 메모리 누수가 발생합니까?

@interface UIStyle : NSObject { 
    NSString * fontName; 
    CGFloat fontSize; 
    UIColor* textColor; 
    UIColor* backgroundColor; 
} 

그래서 난, 같은 레이블이보기에 추가 내가 메모리 누수를 얻을 : UIStyle이 인터페이스를 가지고

UILabel* AddLabel(UIView* view,CGRect labelRect, UIStyle* labelStyle, NSString* labelText) 
{ 
    UILabel* label = [[[UILabel alloc] initWithFrame:labelRect] autorelease]; 
    label.textColor = [UIColor colorWithCGColor:[ labelStyle.textColor CGColor]]; 
    label.backgroundColor = [UIColor colorWithCGColor:[labelStyle.backgroundColor CGColor]]; 
    label.font =[UIFont fontWithName: labelStyle.fontName] size:labelStyle.fontSize]; 

    label.frame = labelRect; 
    label.text = labelText; 
    [view addSubview:label]; 
    return label; 
} 

합니다. 여기 인스트루먼트의 추적입니다 :

0 libSystem.B.dylib calloc 
    1 CoreGraphics CGGlyphBitmapCreate 
    2 CoreGraphics CGFontCreateGlyphBitmap8 
    3 CoreGraphics CGFontCreateGlyphBitmap 
    4 CoreGraphics create_missing_bitmaps 
    5 CoreGraphics CGGlyphLockLockGlyphBitmaps 
    6 0x317c173f 
    7 0x317c3e59 
    8 CoreGraphics CGContextDelegateDrawGlyphs 
    9 CoreGraphics draw_glyphs 
    10 CoreGraphics CGContextShowGlyphsWithAdvances 
    11 WebCore WebCore::Font::drawGlyphs(WebCore::GraphicsContext*, WebCore::SimpleFontData const*, WebCore::GlyphBuffer const&, int, int, WebCore::FloatPoint const&, bool) const 
    12 WebCore WebCore::Font::drawGlyphBuffer(WebCore::GraphicsContext*, WebCore::GlyphBuffer const&, WebCore::TextRun const&, WebCore::FloatPoint&) const 
    13 WebCore WebCore::Font::drawSimpleText(WebCore::GraphicsContext*, WebCore::TextRun const&, WebCore::FloatPoint const&, int, int) const 
    14 WebCore WebCore::Font::drawText(WebCore::GraphicsContext*, WebCore::TextRun const&, WebCore::FloatPoint const&, int, int) const 
    15 WebCore drawAtPoint(unsigned short const*, int, WebCore::FloatPoint const&, WebCore::Font const&, WebCore::GraphicsContext*, WebCore::BidiStatus*, int) 
    16 WebCore -[NSString(WebStringDrawing) __web_drawInRect:withFont:ellipsis:alignment:letterSpacing:lineSpacing:includeEmoji:truncationRect:measureOnly:] 
    17 WebCore -[NSString(WebStringDrawing) _web_drawInRect:withFont:ellipsis:alignment:lineSpacing:includeEmoji:truncationRect:measureOnly:] 
    18 WebCore -[NSString(WebStringDrawing) _web_drawInRect:withFont:ellipsis:alignment:lineSpacing:includeEmoji:truncationRect:] 
    19 UIKit -[NSString(UIStringDrawing) _drawInRect:withFont:lineBreakMode:alignment:lineSpacing:includeEmoji:truncationRect:] 
    20 UIKit -[NSString(UIStringDrawing) drawInRect:withFont:lineBreakMode:alignment:lineSpacing:includeEmoji:] 
    21 UIKit -[NSString(UIStringDrawing) drawInRect:withFont:lineBreakMode:alignment:lineSpacing:] 
    22 UIKit -[UILabel _drawTextInRect:baselineCalculationOnly:] 
    23 UIKit -[UILabel drawTextInRect:] 
    24 UIKit -[UILabel drawRect:] 
    25 UIKit -[UIView(CALayerDelegate) drawLayer:inContext:] 
    26 QuartzCore -[CALayer drawInContext:] 
    27 QuartzCore backing_callback(CGContext*, void*) 
    28 QuartzCore CABackingStoreUpdate 
    29 QuartzCore -[CALayer _display] 
    30 QuartzCore -[CALayer display] 
    31 QuartzCore CALayerDisplayIfNeeded 
    32 QuartzCore CA::Context::commit_transaction(CA::Transaction*) 
    33 QuartzCore CA::Transaction::commit() 
    34 QuartzCore CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) 
    35 CoreFoundation __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ 
    36 CoreFoundation __CFRunLoopDoObservers 
    37 CoreFoundation __CFRunLoopRun 
    38 CoreFoundation CFRunLoopRunSpecific 
    39 CoreFoundation CFRunLoopRunInMode 
    40 GraphicsServices GSEventRunModal 
    41 GraphicsServices GSEventRun 
    42 UIKit -[UIApplication _run] 
    43 UIKit UIApplicationMain 
    44 Cuisine main /iPhone/Projects/iCookProFullSix/iCookPrototype/main.m:14 

그리고이 기능 AddLabel 여러 통화에 대한 그들의 수백이있다.

가 왜 이렇게입니다 .. 누수가 없다 -

AddLabel(self.view, CGRectMake(56, 60, 88, 15), style2, @"text"); 

것은 내가 label.font =[UIFont fontWithName: labelStyle.fontName] size:labelStyle.fontSize]; 를 언급하는 경우이다 : 나는 이런 식으로 전화 ? iOS의 버그 또는 내가 잘못하고있는 것입니까?

P. 이 누출은 기기에서만 볼 수 있습니다.

아무도 도와 줄 수 있습니까? 미리 감사드립니다!

+0

또한 시뮬레이터가 아닌 장치에서만 발생하는 유사한 누출도 발생합니다. – nan

+0

코드에서 clang 정적 분석기를 실행하면 문제를 쉽게 찾을 수 있습니다. –

+0

댓글을 주셔서 감사합니다.하지만 clang으로 앱을 확인한 결과 아무 것도 표시되지 않습니다. – azia

답변

1

당신이 작성한 모든 UIFont에 대해 이러한 누출이 발생하지 않는다면 걱정할 필요가 없습니다. OS가 응용 프로그램을 종료 할 때 누수를 정리합니다.

UIFont이 생성 될 때마다 발생하면 UIStyle 클래스 (이는 Apple의 네임 스페이스와 충돌합니다)는 UIFont을 다시 생성하지 않고 캐시해야합니다.

또한, 당신은 단지 labelStyle.textColor

+0

답장을 보내 주셔서 감사합니다. 귀하의 조언이 도움이 될 수 있는지 확인해 드리겠습니다. – azia

0

가 반환되는 UIfont을 유지하려고 지정할 수 있으며, [UIColor colorWithCGColor:[labelStyle.textColor CGColor]] 비트를 필요로하지 않으며, 수동으로 놓습니다.

나는 이것을 지금 시도하고 그것을 위해 고쳐 쓴 것처럼 보였다.

0

UIStyle 클래스가 글꼴 이름을 올바르게 유지하고 해제하지 않기 때문에 메모리 누수가 발생합니다.

@interface UIStyle : NSObject { 
    NSString * fontName; 
    CGFloat fontSize; 
    UIColor* textColor; 
    UIColor* backgroundColor; 
} 
@property(nonatomic, retain) NSString* fontName; 
... 

또한 UIStyle 구현 파일에서이 속성은 합성되어야하며 dealloc 함수에서도 릴리스되어야합니다.

0

내 이해는 UIFont documentation의 읽기에 기반하여 내재적으로 글꼴 개체가 캐시되므로 동일한 글꼴에 대한 이후 조회가 더 빨리 진행될 수 있습니다. 기억력 상실은 꽤 사소한 것이며, 의도적으로 누수가 될 것 같아 걱정할 것이 없습니다.