سؤال

For this bit of code:

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

For the middle two lines am getting "potential leak of an object" yet I'm not really seeing the problem.

I should mention that the static analyzer is pointing at [self paragraphStyle] which is:

- (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);
}
هل كانت مفيدة؟

المحلول

after the edit it becomes clear. you method is CREATING paragrah styles and those are never released

a) the method should be renamed so it becomes clearer that it creates new objects

b) you have to CFRelease them

what would be enough:

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

*the asserts are bonus

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top