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.