0

对于这段代码:

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

1 回答 1

3

编辑后变得清晰。您的方法是创建段落样式,而这些样式从未发布

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

b) 你必须 CFR 释放它们

什么就足够了:

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

*断言是奖金

于 2013-09-12T23:14:33.087 回答