Objective-C Difference between setting nil and releasing

self.object = nil calls your setter, which will release the old value, set the member to nil, and possibly do other things (it’s a method, so it could do anything). The “anything” part of that is potentially dangerous; see this question, for example.

[object release] releases the old value, but leaves the member as a now-dangling pointer, which is a good recipe for bugs. In dealloc it doesn’t really matter, since the pointer itself is about to go away too, but in any other case it’s a very bad idea to release a member without setting it to nil.

(As a sidenote, you should never assume that releasing an object gives it a reference count of 0. It releases your reference, but other objects may still have references to it.)

Leave a Comment