Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

objective c - Release or set to nil retained members

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;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

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;

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.

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.

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


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.

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...