UIView drawRect与CoreText泄漏

时间:2011-08-16 15:30:48

标签: iphone cocoa-touch memory-leaks core-text

我有一个我无法追查的泄漏。我是CoreText的新手(和一般的C)所以请温柔!静态分析仪没有显示任何问题,但仪器在这种方法中没有:

- (void)drawAttributedStringInBubbleInContext:(CGContextRef)context {
    static CGFloat const kTextInset = 10;

    // Add the text to the bubble using an ellipse path inside the main speech bubble if the text property is set
    if (text) {

        // Create an attributed string from the text property
        NSMutableAttributedString *bubbleText = [[NSMutableAttributedString alloc] initWithString:text];        

        // Justify the text by adding a paragraph style
        CFIndex stringLength = CFAttributedStringGetLength((CFAttributedStringRef)bubbleText);
        CTTextAlignment alignment = kCTJustifiedTextAlignment;
        CTParagraphStyleSetting _settings[] = {
            {kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &alignment}
        };      
        CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(_settings, sizeof(_settings) / sizeof(_settings[0]));
        CFRange stringRange = CFRangeMake(0, stringLength);
        CFAttributedStringSetAttribute((CFMutableAttributedStringRef)bubbleText, stringRange, kCTParagraphStyleAttributeName, paragraphStyle);
        CFRelease(paragraphStyle);      

        // Layout the text within an elliptical frame
        CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)bubbleText);

        // Create elliptical path that is inset from the frame of the view
        CGMutablePathRef path = CGPathCreateMutable();
        CGRect drawingRect = self.bounds;
        drawingRect.origin.x = kTextInset;
        drawingRect.origin.y = kTextInset;
        drawingRect.size.width -= 2 * kTextInset;
        drawingRect.size.height -= 2 * kTextInset;
        CGPathAddEllipseInRect(path, NULL, drawingRect);

        // Create a text frame from the framesetter and the path 
        CTFrameRef textFrame = CTFramesetterCreateFrame(framesetter,CFRangeMake(0,0), path, NULL);

        // Draw the text frame in the view's graphics context
        CTFrameDraw(textFrame, context);

        // Clean up
        CGPathRelease(path);
        CFRelease(framesetter);
        [bubbleText release];
    }
}

根据乐器的主要罪魁祸首是CTFrameRef textFrame =行,虽然我认为我已经正确地释放了所有内容。

1 个答案:

答案 0 :(得分:1)

这是罪魁祸首,你必须发布Core Foundation rule for Create方法。 Apple在Core Text Programming Guide

中的示例中正确发布了它
    // Clean up
    CGPathRelease(path);
    CFRelease(framesetter);
    CFRelease(textFrame);