iphone - 核心基础内存泄漏

标签 iphone ios cocoa-touch cocoa core-foundation

对于这段代码:

        CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)self.fontName, self.paragraphSpacing, NULL);
        [self.text insertAttributedString: [[NSAttributedString alloc] initWithString: @" \n"  attributes: [NSDictionary dictionaryWithObjectsAndKeys: (__bridge id)font, (id)kCTFontAttributeName, (id)[self paragraphStyle], (id)kCTParagraphStyleAttributeName, nil]] atIndex: 0];
        [self.text appendAttributedString: [[NSAttributedString alloc] initWithString: @"\n "  attributes: [NSDictionary dictionaryWithObjectsAndKeys: (__bridge id)font, (id)kCTFontAttributeName, (id)[self paragraphStyle], (id)kCTParagraphStyleAttributeName, nil]]];
        CFRelease(font);

中间两行是“对象的潜在泄漏”,但我并没有真正看到问题所在。

我应该提到静态分析器指向 [self paragraphStyle] 是:

- (CTParagraphStyleRef) paragraphStyle
{
    CTTextAlignment alignment = self.alignment;
    CGFloat lineSpacing = self.lineSpacing;
    CGFloat firstLineHeadIndent = self.indent;
    CGFloat headIndent = self.indent;
    CGFloat tailIndent = -self.indent;

    CTParagraphStyleSetting paragraphSettings[] =
    {
        {kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &alignment},
        {kCTParagraphStyleSpecifierLineSpacing, sizeof(lineSpacing), &lineSpacing},
        {kCTParagraphStyleSpecifierFirstLineHeadIndent, sizeof(firstLineHeadIndent), &firstLineHeadIndent},
        {kCTParagraphStyleSpecifierHeadIndent, sizeof(headIndent), &headIndent},
        {kCTParagraphStyleSpecifierTailIndent, sizeof(tailIndent), &tailIndent},
    };

    return CTParagraphStyleCreate(paragraphSettings, 5);
}

最佳答案

编辑后就清楚了。你的方法是创建段落样式,这些样式永远不会发布

a) 应该重命名该方法,以便更清楚地表明它创建了新对象

b) 你必须 CFRelease 它们

什么就足够了:

CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)self.fontName, self.paragraphSpacing, NULL);
assert(font); //may not be nil
CTParagraphStyleRef paragraph = self.paragraphStyle;
assert(paragraph); //may not be nil
[self.text insertAttributedString: [[NSAttributedString alloc] initWithString: @" \n"  attributes: [NSDictionary dictionaryWithObjectsAndKeys: (__bridge id)font, (id)kCTFontAttributeName, (id)paragraph, (id)kCTParagraphStyleAttributeName, nil]] atIndex: 0];
[self.text appendAttributedString: [[NSAttributedString alloc] initWithString: @"\n "  attributes: [NSDictionary dictionaryWithObjectsAndKeys: (__bridge id)font, (id)kCTFontAttributeName, (id)paragraph, (id)kCTParagraphStyleAttributeName, nil]]];
CFRelease(font);
CFRelease(paragraph); //!!!

*断言是奖励

关于iphone - 核心基础内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18775703/

相关文章:

iphone - objective-c 开关语句中的OR运算符

iphone - 是否可以调整 iPhone 的振动级别?

php - 如何从 php 向 iphone 发送正确的 html 数据?

ios - 应用程序在 ios 的启动屏幕或启动屏幕中被击中

ios - 崩溃: Trying to complete an interactive gesture but the animation coordinator is nil

iphone - 如何为包含 18000 个条目的 Sqlite 数据库表建立索引

objective-c - 使用 NSDateFormatter 解析日期

iphone - iOS类方法的使用

ios - 如何在 IOS 8 Autolayout 中安装/复制大小类的所有约束到另一个

全国 iOS 离线 map (OSM)