iphone - CFAttributedStringRef 中的内存泄漏

标签 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);

}

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

最佳答案

您还需要释放 CFAttributedStringRef,但我没有看到您已在方法中释放它。该函数的所有权遵循创建规则

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

关于iphone - CFAttributedStringRef 中的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9311871/

相关文章:

iphone - 如何阻止我的应用程序为每个单元格多次访问数据库?

objective-c - NSView 在 NSSplitView 上设置最小尺寸

iphone - 如何在 Cocos2D 中显示抛物线运动以及如何找到 Sprite 的最终位置

c# - 服务器无法在发送 HTTP header 后附加 header c# pdf inline

iphone - Facebook 相当于 iOS 上的 TWTweetComposeViewController?

ios - iphone os 5 - 发送到实例的无法识别的选择器

iphone - 使用 iOS-libarchive 库提取 .tar 文件时出现问题

iphone - 如何转移View但不关闭View数据?

html - 在 PDF 的封面上隐藏页眉/页脚

string - Matlab:字符串不是真正的字符串?