It depends on whether the property is retain
or not. Most object properties are retained; this is what a retain property looks like:
- (void)setBlueViewController:(BlueViewController *)bvc {
if (bvc != blueViewController) { // blueViewController is local ivar
[blueViewController release];
blueViewController = [bvc retain];
}
}
So what you're doing up there is creating a retain count of +2. When you init
, that's a +1; the property then retain
s it, bumping it up to +2. Your dealloc
releases it once, which brings it down to +1...and you've leaked that property. Because you are alloc
/init
-ing the variable, you don't want to use the setter; instead, assign it directly to the instance variable.
By instantiating it directly, it saves you the trouble of that other release
—fewer lines of code means fewer errors. You might, for example, have typed retain
by accident and not realized it until your program crashes because you retained a massive class...
Of course, as Caleb says you could autorelease, but that's effectively letting the object lie around in memory until the run loop is finished. It's much easier, and gives you more control, to just not worry about that. There's nothing wrong with assigning the alloc
/init
to the ivar; in fact, that's the best way to do it.