CFAttributedStringRef中的内存泄漏

时间:2012-02-16 13:12:38

标签: iphone objective-c pdf memory memory-leaks

我使用以下方法在PDF文档(上下文)上绘制文本。 以某种方式仪器出现以下行泄漏

CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL);

我在哪里发布stringRef。这是代码(在正确答案之后是更新/工作代码):

- (void)drawText:(NSString*)textToDraw inFrame:(CGRect)frameRect{

CFStringRef stringRef = (__bridge CFStringRef)textToDraw;
// Prepare the text using a Core Text Framesetter
CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);

CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, frameRect);

// Get the frame that will do the rendering.
CFRange currentRange = CFRangeMake(0, 0);
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
CGPathRelease(framePath);

// Get the graphics context.
CGContextRef    currentContext = UIGraphicsGetCurrentContext();

// Put the text matrix into a known state. This ensures
// that no old scaling factors are left in place.
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);

// Core Text draws from the bottom-left corner up, so flip
// the current transform prior to drawing.
CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2);
CGContextScaleCTM(currentContext, 1.0, -1.0);

// Draw the frame.
CTFrameDraw(frameRef, currentContext);

CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2);

CFRelease(frameRef);
//CFRelease(stringRef); The string shouldn't be released
CFRelease(framesetter);
//Added the next line:
CFRelease(currentText);

}

在设备上测试它作为模拟器;它泄漏了

1 个答案:

答案 0 :(得分:2)

您还需要释放CFAttributedStringRef,但我没有看到您已在方法中发布它。此功能的所有权遵循创建规则

https://developer.apple.com/library/mac/#documentation/CoreFoundation/Conceptual/CFMemoryMgmt/Concepts/Ownership.html#//apple_ref/doc/uid/20001148-103029