xcode - 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 这一切都好,而不是抱怨它?
解决方案
好的,鉴于这里的讨论是我认为可以解决分析器投诉的方法,让您保留您的正式财产,并且不违反任何内存管理规则。
声明一个只读属性:
@property (readonly) CGContextRef cacheContext;
创建时直接分配 ivar
_cacheContext = [self allocContextWithImage:self.someImage];
释放它dealloc
:
- (void)dealloc
{
CGContextRelease(_cacheContext);
[super dealloc];
}
推荐阅读
- python - 无法访问 ko foreach 内容中的元素
- python - 这个语法是什么意思?words = [stemmer.stem(w.lower()) for w in words if w != "?"]
- android - 键入时间选择器不会触发 onTimeSet
- rest - Rest Easy Swagger api 文档
- reactjs - React 获取代码镜像值 onClick
- ios - How to decide what to use [Sqlite, Realm, CoreData, User-default, JSON file] to store data iOS?
- tensorflow - tape.gradient() 不能运行两次?
- r - 用R中的字符串替换作为整数向量列表(不仅仅是单个整数)的数据框列中的整数
- c++ - 获得基于 BSD 套接字的程序在 Emscripten 下进行 LAN 联网需要什么?
- c++ - 我如何将 std::max_element 用于结构