2011-08-16 51 views
1

我有一個泄漏,我無法追查。我是CoreText(和一般C)的新手,所以請溫柔!靜態分析儀不顯示任何問題,但儀器確實在這個方法:UIView drawRect漏洞與CoreText

- (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

這是罪魁禍首,Core Foundation rule for Create方法是你必須釋放它們。 Apple在Core Text Programming Guide的示例中正確地發佈了它。

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

感謝。工作過一種享受。不能相信我錯過了這一點。欣賞快速反應,推理和額外的鏈接。乾杯戴夫。 –