gpt4 book ai didi

iphone - CFAttributedStringRef 中的内存泄漏

转载 作者:行者123 更新时间:2023-12-03 21:04:02 24 4
gpt4 key购买 nike

我使用以下方法在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com