Is it necessary to release the ivar and set the synthesized ivar to nil at the same time?

objective-c

Solution

You're right: you can indeed do `self.property = nil` everywhere, including dealloc. The only downside is that if the setter method does anything more complicated than just releasing the ivar, you might end up trying to access other fields that have already been released, etc.

As for why you also release the outlet in `viewDidUnload`, that's a memory optimization. Since the stuff you release in `viewDidUnload` are things that will be reinstantiated when the view is loaded again, releasing them there frees up memory in low-memory situations.

Problem

I have seen code (probably Apple's own sample code) written in such a way where it releases the ivar in dealloc and setting the property in viewDidUnload. e.g. ``` - (void)viewDidUnload { self.navigationController = nil; } - (void)dealloc { [_navigationController release]; } ``` Why do them in two places? Also, why set nil in one and release in another. It seems that self.property = nil would just take care of everything since it would release and set the ivar to nil.

Original source