释放或设置为零个保留成员 [英] Release or set to nil retained members

查看:103
本文介绍了释放或设置为零个保留成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将我的保留成员vars设置为nil或在清理时释放它们是更好的选择吗?将保留的var设置为nil似乎是一种释放对象的更安全的方法,而无需冒险对其进行两次释放.

Is it better to set my retained member vars to nil or to release them when I am cleaning up? Setting a retained var to nil seems a safer way to release an object without risking a double release call on it.

更新:让我详细说明一下,我指的是已设置为具有保留属性的成员变量,即:

Update: Let me elaborate that I'm referring to member vars that have been set to have the retain property i.e.:

@property (nonatomic, retain) SomeClass* mInstanceVar;

推荐答案

最佳做法是先释放实例变量,然后在-dealloc方法中将其设置为nil.我个人喜欢这样:

It's best practice to release your instance variables first, and then set them to nil in your -dealloc method. I personally like doing that like so:

[myVar release], myVar = nil;

如果将实例变量设置为nil,则不会释放它们,而是会导致内存泄漏.不过,在释放后将它们设置为nil可以确保您不会引起泄漏,并且如果由于某种原因稍后尝试访问那些实例变量,则不会获得垃圾内存.

If you set your instance variables to nil, you're not releasing them, and you're causing a memory leak. Setting them to nil after releasing though will ensure that you're not causing leaks, and if, for some reason, you try to access those instance variables later, you're not going to get garbage memory.

如果您设置了这样的实例变量,

If you have an instance variable set up as such,

@property (retain) NSObject *myVar;

然后在释放过程中调用self.myVar = nil;不是一个好主意.如果您的实例变量上已注册了KVO通知的对象,则调用self.myVar = nil将发送这些通知,其他对象将得到通知,这很糟糕,因为它们希望您仍然处于有效状态,您可以如果您正在解除分配过程中,则不会.

then it is not a good idea to call self.myVar = nil; during deallocation. If you have objects that have registered for KVO notifications on your instance variable, calling self.myVar = nil will send those notifications, and other objects will be notified, which is bad because they will be expecting you to still be in a valid state—you're not if you're in the deallocation process.

即使没有为KVO通知注册它们,这样做也不是一个好主意,因为当对象状态不一致时,您永远不要调用可能依赖于对象状态的方法(某些变量可能/将不存在) ,您应该自己亲自处理该过程. [myVar release], myVar = nil;就足够了.

Even if they're not registered for KVO notifications, it's still not a good idea to do that because you should never call methods that could rely on your object's state when its state is inconsistent (some variables might/will be nonexistent), and you should simply handle the process yourself. [myVar release], myVar = nil; will suffice.

如果您想了解更多信息,请阅读Dave DeLong对问题.

If you want more information, read Dave DeLong's answer to this question.

对于初始化,调用属性设置器和获取器也是不好的(原因与上面的原因相同).在-init调用中,您将这样设置上述变量:

For initializing, it is also not good to call property setters and getters (for much the same reason as above). In an -init call, you would set up the aforementioned variable as such:

myVar = nil; // If you want to set it up as nil.
OR
myVar = [[NSObject alloc] init]; // Or set it up as an actual object.

在类处于不确定状态的情况下,请避免使用self.myVar = nilself.myVar = [[NSObject alloc] init(这些调用在-viewDidLoad-awakeFromNib中很好,但是,因为到那时,您的类已被完全初始化,并且您可以依靠实例变量来使其处于完整状态).

Avoid self.myVar = nil and self.myVar = [[NSObject alloc] init in cases where your class is in an undeterminate state (these calls are fine in -viewDidLoad and -awakeFromNib, though, because by that point, your class has been completely initialized, and you can rely on the instance variables to be in a complete state).

这篇关于释放或设置为零个保留成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆