首页 > 解决方案 > Xcode 分析器抱怨 CFContextRef 存储在“assign”@property 中

问题描述

我有一个 Cocoa 类,它需要长时间保留位图上下文来进行像素操作。

@property (assign, nonatomic) CGContextRef cacheContext; // block of pixels

在我的课堂初始化中:

// this creates a 32bit ARGB context, fills it with the contents of a UIImage and returns a CGContextRef
[self setCacheContext:[self allocContextWithImage:[self someImage]]];

在dealloc中:

CGContextRelease([self cacheContext]);

Xcode 分析器公司关于 init 泄漏了 CGContextRef 类型的对象,并且在 dealloc 中抱怨“不正确地递减不属于调用者的对象”。

我相信这一切都很好,并且运行良好。

我如何告诉 Xcode 这一切都好,而不是抱怨它?

标签: xcodecocoacore-foundationanalyzer

解决方案


好的,鉴于这里的讨论是我认为可以解决分析器投诉的方法,让您保留您的正式财产,并且不违反任何内存管理规则。

声明一个只读属性:

@property (readonly) CGContextRef cacheContext;

创建时直接分配 ivar

_cacheContext = [self allocContextWithImage:self.someImage];

释放它dealloc

- (void)dealloc
{
    CGContextRelease(_cacheContext);
    [super dealloc];
}

推荐阅读