核心基础内存泄漏

时间:2013-09-12 23:06:10

标签: 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);
}

1 个答案:

答案 0 :(得分:3)

编辑后,它变得清晰。你的方法是创建paragrah样式而那些永远不会发布

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

b)你必须CF发布它们

什么就足够了:

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); //!!!

*断言是奖金