2010-07-14 73 views
4

我有功能添加的UILabel我的看法:iPhone iOS4上的內存泄露?

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; 
} 

其中UIStyle有這個接口:

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

所以當我添加標籤等來查看,我得到的內存泄漏。 這裏是從儀器跟蹤:

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.S.此泄漏只在設備上可見。

任何人都可以幫忙嗎?提前致謝!

+0

我也遇到類似的泄漏,也只發生在設備上,而不是在模擬器上。 – nan 2010-07-14 17:11:43

+0

在您的代碼上運行鐺靜態分析器可能會幫助您找到問題。 – 2010-07-14 19:56:21

+0

感謝您的評論,但我用叮噹檢查了應用程序,並沒有顯示任何內容。 – azia 2010-07-15 05:09:54

回答

1

除非您創建的每個UIFont都發生此泄漏,否則不必擔心 - 操作系統將在您的應用程序退出時清除泄漏。

如果每次創建UIFont時有發生,也許你的UIStyle類(與蘋果的命名空間的方式碰撞)是否應緩存UIFont而不是它重新的。

而且,你不需要[UIColor colorWithCGColor:[labelStyle.textColor CGColor]]位,你可以指定labelStyle.textColor

+0

謝謝你的回答。我會檢查你的建議是否可以提供幫助! – azia 2010-07-14 18:49:07

0

嘗試保留所返回的UIfont,並手動將其釋放。

我剛纔試過這個,它似乎爲我修好了。

0

內存泄漏是由於UIStyle類不保留並正確釋放字體名稱導致的。它應該是:

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

此外,在UIStyle實現文件屬性應該被合成並同時發佈的dealloc功能。

0

我的理解基於對UIFont documentation的閱讀,字體對象在內部被緩存,以便將來可以更快速地查找相同字體。我相信內存的損失相當小,而且可能的泄漏似乎實際上是設計上的,所以沒有什麼可擔心的。