Xcode分析器抱怨CFContextRef存储在“assign”@属性中

Xcode分析器抱怨CFContextRef存储在“assign”@属性中,xcode,cocoa,core-foundation,analyzer,Xcode,Cocoa,Core Foundation,Analyzer,我有一个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 setCacheContex

我有一个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 analyzer发现init泄漏了一个CGContextRef类型的对象,在dealloc中,有一个关于调用方不拥有的对象的错误递减的投诉

我相信这一切都是好的,它运行完美


我怎样才能告诉Xcode这一切都没问题而不抱怨呢?

好吧,鉴于这里的讨论,我认为这将解决analyzer的抱怨,让您保留您的正式属性,并且不违反任何内存管理规则

声明只读属性:

@property (readonly) CGContextRef cacheContext;
创建ivar时直接分配它

_cacheContext = [self allocContextWithImage:self.someImage];
在dealloc中释放它:

CGContextRelease([self cacheContext]);

ARC还是托管内存?另外,cacheContext getter和setter的代码是什么?这是非ARC代码,getter/setter只是@PropertyAsign提供的内置代码,非原子。在init中,警告已分配类型为“CGContextRef _Nullable”的对象稍后不会在此执行路径中引用,并且保留计数为+1。确实,它在dealloc之前不会被进一步引用,因为它现在存储在一个assign属性中,以便在类的其他地方使用。如果您使用生成的getter/设置,那么您不需要非原子的。试着把它拿出来看看是否有用。我还将尝试删除assign属性,这不会对分析器造成伤害,看看这是否会让分析器满意。一个问题是,您的保留是不对称的—保留的setter应该在dealoc时释放,而不保留的setter不应该释放。删除assign和/或非原子的setter没有帮助。我正在创建CFContext并将其存储在assign属性中——实际上只是类的一个局部变量。我在dealloc中正确地释放了它,以便它在我的类对象的生命周期中持续存在。我的setters/getter不应该保留或发布任何内容-CFContextRef是我所有的,我需要发布它,我确实。。。。没有评论室了。。。我的建议是将setter更改为retain或roll-your-one使用CFRetain,然后使用CGContextRef ctx=[self-alloctContextWithImage:…];self.cacheContext=ctx;CGContextReleasectx;现在您使用的是平衡的保留/发布,将来可能会避免一个bug!