2012-02-16 129 views
2

我使用以下方法在PDF文檔(上下文)上繪製文本。 不知怎的,儀器自帶了以下行泄漏CFAttributedStringRef中的內存泄漏

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

}

測試它的設備的模擬器上;它泄漏

回答

2

您還需要釋放CFAttributedStringRef,並且我沒有看到您已經在方法中釋放它。此功能的所有權遵循創建規則

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

+0

感謝您的快速響應。當我添加CFRelease(currentText)我的應用程序崩潰..嗯 – Oritm 2012-02-16 13:32:37

+0

你可以發佈錯誤?當它被其他東西訪問時(比如framesetter),很可能就是它。 – 2012-02-16 13:35:24

+0

它只是崩潰,但當我有NSZombies啓用它提出:*** - [CFString釋放]:消息發送到釋放實例0xf6534c0 – Oritm 2012-02-16 13:38:43