对于这段代码:
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/