#define TT_RELEASE_SAFELY(__POINTER) { [__POINTER release]; __POINTER = nil; }
Why is does three20 consider it safe to assign an ivar to nil after releasing it? Is it unsafe to leave out the ivar = nil
step?
This is all I found: http://github.com/facebook/three20/commit/1b946f475fb28d60e0aafc9ef394050c642c3a5b#commitcomment-115517
I don't think I'm using KVO/KVC, but I'm not really sure. I'm reading up on it now.
Thanks!
Matt
When inside -dealloc
, this question splits the Objective-C gurus. read this recent blog entry for example.
When inside an implementation of other methods, my personal opinion is that you shouldn't keep the variable in scope after the release in the first place. This code
SomeClass* someObject= ...
... use someObject ...
[someObject release];
... more code ...
might accidentally access someObject
later in code, thus leading to a crash. So you might say
SomeClass* someObject= ...
... use someObject ...
[someObject release];
someObject=nil;
... more code ...
would be better, because messaging to nil
is harmless.
However, in this case you can remove the danger altogether:
{
SomeClass* someObject= ...
... use someObject ...
[someObject release];
}
... more code ...
Here I'm using a {...}
block to limit a scope of a variable. Then the use of someObject
later is simply a compile-time error.