目标C-内存管理问题? [英] Objective C - Memory management question?
问题描述
我知道,当我拥有设置为保留或复制的对象的属性时,必须执行以下操作.
I know that when i have a property for an object which is set to retain or copy i must do the following.
Object *o = [[Object alloc] init];
self.myObject = o;
[o release];
但是如果我没有为myObject定义属性怎么办?
but what if i don't define a property for myObject?
我可以进行以下操作而不会泄漏吗?
Can i do the following without having a leak?
为每个班级中的每个变量定义一个属性是否更好? 为什么?
Is it better to define a property for every variable I have in every class? Why??
myObject = [[Object alloc] init];
推荐答案
您可以做的是确保在将拥有该对象的变量分配给新实例之前,先释放先前分配的对象(该对象是另一个对象的保留版本,或者副本).
What you can do is ensure you release the previously allocated object before assigning the variable holding it to a new instance (be it a retained version of another object, or a copy of it).
如果您没有为其定义属性,请不要使用点语法.
If you don't define a property for it, don't use the dot syntax.
这篇关于目标C-内存管理问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!