【问题标题】:UIView drawRect leak with CoreTextUIView drawRect 泄漏与 CoreText
【发布时间】:2011-08-16 15:30:48
【问题描述】:

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

- (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];
    }
}

根据 Instruments 的说法,罪魁祸首是 CTFrameRef textFrame = 行,尽管我认为我已经正确发布了所有内容。

【问题讨论】:

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


    【解决方案1】:

    这是罪魁祸首,Core Foundation rule for Create 方法是你必须释放它们。 Apple 在Core Text Programming Guide 的示例中正确发布了它。

        // Clean up
        CGPathRelease(path);
        CFRelease(framesetter);
        CFRelease(textFrame);
    

    【讨论】:

    • 谢谢。工作了一个款待。不敢相信我错过了这个。感谢快速响应、推理和额外链接。干杯戴夫。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多