iPhone iOS4上的内存泄漏?

4

我有一个添加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;
}

当我将这样的标签添加到视图中时,会出现内存泄漏。以下是来自Instruments的跟踪:

   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系统的一个bug吗还是我的操作有误?

顺便提一下,这个内存泄漏只在设备上可见。

可以有人帮忙吗?先谢谢了!


我也遇到了类似的泄漏问题,而且只在设备上出现,模拟器上没有。 - nan
运行clang静态分析器可以帮助您定位问题。 - Jon Shier
谢谢您的评论,但我已经使用clang检查了应用程序,并且没有显示任何问题。 - azia
4个回答

1

除非每次创建UIFont都会发生这种泄漏,否则不必担心 - 当您的应用程序退出时,操作系统将清理泄漏。

如果每次创建UIFont时都会发生这种情况,也许您的UIStyle类(顺便说一下,它与 Apple 的名称空间冲突)应该缓存UIFont而不是重新创建。

此外,您不需要[UIColor colorWithCGColor:[labelStyle.textColor CGColor]]代码段,您可以直接赋值labelStyle.textColor


谢谢您的回答。我会检查一下您的建议是否有帮助! - azia

0

内存泄漏是由于UIStyle类没有正确地保留和释放字体名称所致。应该是:

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

此外,在UIStyle实现文件中,该属性应该被合成,并在dealloc函数中释放。

0
根据我阅读的UIFont文档,我的理解是字体对象会被内部缓存,以便于未来查找相同字体时可以更快地进行。我相信内存损失相当小,而潜在的泄漏似乎实际上是设计上的,所以不用担心任何问题。

0

尝试保留返回的UIfont,并手动释放它。

我刚刚尝试了这个方法,似乎对我有用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接