2011-08-16 5 views
1

У меня есть утечка, которую я не могу отследить. Я новичок в CoreText (и C в целом), поэтому, пожалуйста, будьте нежны! Статический анализатор не показывает каких-либо проблем, но инструменты делает в этом методе:UIView drawRect leak с 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

спасибо. Работал. Не могу поверить, что я пропустил это. Оцените быстрый ответ, рассуждения и дополнительные ссылки. Привет, Дейв. –

Смежные вопросы