Vignesh Babu
Vignesh Babu

Reputation: 690

What is the difference between setting object = nil and [object release] VS [object release] and object = nil?

What is the difference between these two code snippets:

object = nil;
[object release]

Vs

[object release];
object = nil;

which is the best practice ?

Upvotes: 2

Views: 467

Answers (1)

Oliver
Oliver

Reputation: 23510

object = nil; 
[object release]

Don't do that. You are sending a release message on a nil object that will just do nothing. But the object that was referenced by your object is still in memory because it has never received a release message.

[object release]; 
object = nil;

Here you release the object, and for convenience and security, you set nil to its reference. So you can call (by mistake of course :-) ) any method on that object and the app won't crash.

But if you use a retained property @property(nonatomic, retain), calling :

self.object = nil;

equals to call :

[object release]; 
object = nil;

Upvotes: 9

Related Questions