CIFilter
CIFilter

Reputation: 8677

Is It Necessary to Set Pointers to nil in Objective-C After release?

Is there anything wrong with doing something like

NSString * string = [ [ NSString alloc ] init ];
...
[ string release ];

or is there any value (other than best practice) in also adding

string = nil;

?

Upvotes: 22

Views: 5964

Answers (3)

Vagrant
Vagrant

Reputation: 1716

Objective-C is really the same as C with a fancy preprocessor.

Setting a pointer to nil in Objective-C has no effect on what was once pointed to by that pointer.

Upvotes: 0

BJ Homer
BJ Homer

Reputation: 49034

Setting an instance variable to nil is more useful in a multi-threaded application than a single-threaded one, since with multiple threads you can't always guarantee that an instance variable will only be read before it's released.

I generally don't bother in single-threaded applications, unless there's some other compelling reason.

Upvotes: 3

Paul Tomblin
Paul Tomblin

Reputation: 182802

Not necessary, but good practice. If you were to inadvertently reference it after release, bad things could happen, but in Objective C there isn't any harm in referencing a nil.

Upvotes: 29

Related Questions