When you use Xcode’s feature and drag from the nib file into the .h and .m file, Xcode adds the code in the dealloc and viewDidUnload. It adds extra code I do not normally add. I am just curious if this extra code is needed.
I would have done [self setDisplaySlider:nil] and not disp = nil and [disp release].
Is this necessary? I don’t think you have to release disp.
@interface ViewController : UIViewController
{
IBOutlet UISegmentedControl *disp;
}
@property (retain, nonatomic) IBOutlet UISegmentedControl *displaySlider;
@end
- (void)viewDidUnload
{
[self setDisplaySlider:nil];
[disp release];
disp = nil;
[super viewDidUnload];
}
- (void)dealloc {
[displaySlider release];
[disp release];
[super dealloc];
}
In my opinion you provided a class with extra code. I’ll try to explain.
First of all, in your previous you have two different
IBOutlet. I think that the first one has been added by you.The second one, instead, has been added by Xcode when you made the drag and drop oeration.
First consideration
The term
IBOutletis only a placeholder for Xcode. By means of it, Xcode helps you to connect an instance variable to a graphical element.When I use outlet connections I usually supplying an accessor like
@property (retain, nonatomic) IBOutlet UISegmentedControl *displaySlider;(as Xcode did) because if not you could incurr in memory leak problems.Second consideration
The code Xcode has provided is right. When you made the drag operation you linked toghether
displayControllerinstance variable with your graphical element. To balance this connection, that instance variable has to be released in dealloc method like the following:In addiction, Xcode added
[self setDisplaySlider:nil];because in memory warning situationsviewDidUnloadmethod could be called. No problem here because whene the controller is loaded in memory again, the outlet connection is restored.The difference between the two method calls can be read in Advanced Memory Management doc and release-or-set-to-nil-retained-members. Note that if you do this:
you access directly your instance variable called
displayController, while if you do this:you access the accessor (the setter method in this case) for that instance variable. It’s not the same (to avoid confusion see the code I provided).
So, this is the code I would use (I added some comments to guide you):
Hope it helps.