我有这个方法(别人写的!)如何正确释放CF对象对象?
- (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创建或复制CF函数名称来释放所有东西。我不明白的是我如何发布pdf,并仍然能够在函数结束时返回它。 我错过了什么? 谢谢。
谢谢。所以当CGPDFDocumentRef对象在最后返回时,其保留计数不会改变,对吧?它仍然是一个,它是来电者的责任,使它成为0(使用它之后) – TrekOnTV2017
是的,这是正确的。 –