gpt4 book ai didi

objective-c - 如何正确释放一个CF对象对象?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:52:21 25 4
gpt4 key购买 nike

我有这个方法(别人写的!)

- (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;
}

现在,我运行了 Analyze 并收到了三个内存泄漏警告:

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 并且仍然能够在函数结束时返回它。我错过了什么?谢谢。

最佳答案

您需要像此处所示的 clang 源注释 http://cocoasamurai.blogspot.com/2012/01/clang-source-annotations.html

    #import <AppKit/AppKit.h>
@interface NSColor (CWNSColorAdditions)
-(CGColorRef)cw_cgColor CF_RETURNS_RETAINED;
@end

关于objective-c - 如何正确释放一个CF对象对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11038264/

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