我有一个Cocoa类,需要长时间保持位图上下文以进行像素操作。
@property (assign, nonatomic) CGContextRef cacheContext; // block of pixels
在我的班级init中:
// 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一切正常,而不必抱怨呢?
最佳答案
好的,考虑到这里的讨论,我认为这将解决分析器的问题,让您保留自己的正式财产,并且不违反任何内存管理规则。
声明一个只读属性:
@property (readonly) CGContextRef cacheContext;
创建后直接分配ivar
_cacheContext = [self allocContextWithImage:self.someImage];
在
dealloc
中释放它:- (void)dealloc
{
CGContextRelease(_cacheContext);
[super dealloc];
}
关于xcode - Xcode分析器提示CFContextRef存储在“assign” @property中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59439365/