I\'m sorting out some memory issues with my iPhone app and I\'ve just been thinking about some basics. If I setup an ivar and never end up using it in the lifespan of my obj
I find that it is good practice to always set those ivars to nil
in the init
method. That way, you are absolutely sure that your call to release
in the destructor can not cause problems.
If it turns out that Objective-C does automatically set them to nil
, and for some reason you find yourself with a speed bottleneck that can be improved upon by removing those assignments (highly unlikely), then you can worry about removing them. In the meantime, set them all to nil
and sleep easier :)
update: BJ Homer and Chuck have pointed out that the ivars will automatically be set to zero, so now it comes down to a decision on style.
Instance variables are initialized to 0 before your initializer runs..
Yes, ivars are always initialized to 0/nil/NULL/NO/etc.
However, if it helps you understand what's going on, go for it. The performance impact is negligible. You don't need to do it, but it won't cause any problems if you do.