CGImageRef 属性保留或不保留
我有一个关于如何将 CGImageRef 作为类的合成属性处理的问题。 如果我定义一个 CGImageRef ,
@property (nonatomic, retain) CGImageRef image;
那么编译器会抱怨这里不能使用“retain”。如果我省略保留,那么我假设使用“分配”,并且在设置属性时我需要自己保留:
self.image = CGImageRetain ( cgimage );
然后在运行分析时收到“潜在泄漏”警告。我可以安全地忽略这个警告吗?或者,即使属性定义中没有指定“retain”,合成代码仍然会执行隐式 CGRetain 吗?
I have a question on how to handle a CGImageRef as a synthesized property of a class.
If I define an CGImageRef with
@property (nonatomic, retain) CGImageRef image;
then the compiler complains that "retain" cannot be used here. If I leave out the retain, then I assume "assign" is used instead, and I need to do the retain myself when I set the property:
self.image = CGImageRetain ( cgimage );
then I get an "Potential leak" warning when running Analyze. Can I safely ignore this warning? Or does the synthesize code do an implicit CGRetain anyways, even though no "retain" is specified in the property definition?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您要做的就是向属性添加一个注释,表明该类型确实可以保留。
将属性声明更改为
注意,这只会为您生成 getter 和 setter,实例变量本身不受 ARC 控制。具体来说,这意味着您必须在dealloc中释放它,并且在直接分配给实例变量时需要使用适当的retain和release。
更好的方法可能是使用
typedef
:在这种情况下,实例变量由 ARC 控制,因此您必须 不会在
dealloc
中释放它,并且对实例变量的直接赋值也由ARC处理。有关参考,请参阅规范,特别是第 4.1.1 节:
和第 3 节:
What you want to do is add an annotation to the property that the type really can be retained.
Change the property declaration to
Note that this will only generate the getters and setters for you, the instance variable itself is not ARC controlled. Specifically, this means that you must release it in
dealloc
, and that you need to use proper retain and release when assigning directly to the instance variable.A better approach may be to use a
typedef
:In this case, the instance variable is controlled by ARC, and so you must not release it in
dealloc
, and direct assignments to the instance variable are handled by ARC as well.For reference, see the specification, specifically section 4.1.1:
and section 3:
我不喜欢在编译时指示编译器。我认为这很丑。我会自己重写这些方法。
I don't like to instruct the compiler when compiling. I think it's ugly. I'd override the methods myself.
这个怎么样?
How about this?