views:

485

answers:

3

Hi there,

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 object, when I call dealloc on it, will that cause a problem? E.g.

@interface testClass {
    id myobject;
}
@property (nonatomic, retain) id myobject;
@end

@implementation testClass
@synthesize myobject;
- (id)init {
    ...
    // Do I have to set myobject to nil here?
    // So if myobject isn't used the dealloc call to nil
    // will be okay? Or can you release the variable without
    // having set every object to nil that you may may not use 
    ...
}

...

// Somewhere in the code, myobject may be set to
// an instance of an object via self.myobject = [AnObject grabAnObject]
// but the object may be left alone

...

- (void)dealloc {
    [myobject release];
    [super dealloc];
}
@end
A: 

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 worray about removing them. In the meantime, set theem 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.

e.James
You can be sure either way. Just one way your init method is full of a useless assignments.
Chuck
Thanks for your answer :-)
Michael Waterfall
+2  A: 

Yes, ivars are always initialized to 0/nil/NULL/NO/etc.

However, explicitly doing it is good practice, anyway. The performance impact is extremely minimal, and then you'll be explicit about what you're doing.

BJ Homer
I don't see how wasted, redundant code is good practice. Do you make sure to set all your local variables to the same value several times in a row just to make it really explicit?
Chuck
No. Setting local variables multiple times would reduce the clarity of the code and increase confusion. Explicitly setting an ivar increases clarity, and a new member of the team less familiar with objc understand more easily what's happening. Code clarity is more important than reducing "wasted" code.
BJ Homer
Objective-C developers should be familiar with the zero-initialization of ivars. Due to zeroing `init` methods can often be omitted completely, which is a good thing. If developers are still learning they shouldn’t learn this concept from code but from a book, instructor, SO or whatever. They would not learn this concept by reading redundant code, anyway.
Nikolai Ruhe
This is bad advice. There is absolutely no reason to set your ivars to nil.
retainCount
+14  A: 

Instance variables are initialized to 0 before your initializer runs..

Chuck
Thanks for your answer and the link!
Michael Waterfall