我有这个方法(别人写的!)
- (CGPDFDocumentRef)getPdf {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@"myLocalFileName.pdf"];
NSURL *pdfURL = [NSURL fileURLWithPath:pdfPath];
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
return pdf;
}
现在,我运行了分析并收到三个内存泄漏警告:
Call to function 'CGPDFDocumentCreateWithURL' returns a Core Foundation object with a +1 retain count
Object returned to caller as an owning reference (single retain count transferred to caller)
Object leaked: object allocated and stored into 'pdf' is returned from a method whose name ('getPdf') does not start with 'copy', 'mutableCopy', 'alloc' or 'new'. This violates the naming convention rules given in the Memory Management Guide for Cocoa
有人可以告诉我这里需要/应该做什么吗?我知道我应该在 CF 函数名称中使用 create 或 copy 来 CFRelease 一切。我不明白的是如何释放 pdf 并且仍然能够在函数结束时返回它。我错过了什么?谢谢你。