[英]memory leak on iPhone iOS4?
我有將UILabel添加到我的視圖的功能:
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的錯誤還是我做錯了什么?
PS此泄漏僅在設備上可見。
有人可以幫忙嗎? 提前致謝!
除非您創建的每個UIFont
都發生此泄漏,否則不必擔心 - 操作系統將在您的應用程序退出時清除泄漏。
如果每次創建UIFont
時都會出現,可能你的UIStyle
類(順便UIFont
Apple的命名空間)應該緩存UIFont
而不是重新創建它。
另外,你不需要[UIColor colorWithCGColor:[labelStyle.textColor CGColor]]
位,你可以只指定labelStyle.textColor
嘗試保留返回的UIfont,並手動釋放它。
我剛試過這個,它似乎為我解決了。
引起內存泄漏是因為UIStyle類沒有正確保留和釋放字體名稱。 它應該是:
@interface UIStyle : NSObject {
NSString * fontName;
CGFloat fontSize;
UIColor* textColor;
UIColor* backgroundColor;
}
@property(nonatomic, retain) NSString* fontName;
...
另外,在UIStyle實現文件中,該屬性應該合成並在dealloc函數中釋放。
基於對UIFont文檔的閱讀,我的理解是在內部緩存字體對象,以便將來可以更快地查找相同字體。 我確信內存中的損失相當小,而且可能泄漏似乎實際上是設計的,所以不用擔心。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.