我有这种方法(别人写的!)
- (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并仍然能够在函数末尾将其返回。
我想念什么?
谢谢。
最佳答案
完成后,接收pdf的代码有责任将其CFRelease删除。与Cocoa不同,CF不支持自动释放,因此从CF函数返回的任何对象都是调用者拥有并必须处理的对象。
同样是命名约定,那些返回CF对象的函数应使用create
或copy
相应地命名。
关于objective-c - 如何正确释放CF对象对象?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11038264/