views:

151

answers:

1

A quick question, after viewDidUnload does the dealloc also get called? I am asking with regards to pickerData, it was my understanding that the variable would be released when the dealloc gets called. My reason for asking is that I have noticed in one book that the author sets pickerData to nil in the viewDidUnload. Is this harmless overkill, maybe even good practice, or is there no scenario where one would not be called without the other.

INTERFACE:

@interface SingleViewController : UIViewController {
    NSArray *pickerData;
}
@property(nonatomic, retain) NSArray *pickerData;
@end

IMPLMENTATION:

-(void)viewDidUnload {
    [self setSinglePicker:nil];
    [self setPickerData:nil]; 
    [super viewDidUnload];
}

-(void)dealloc {
    NSLog(@"Here");
    [singlePicker release];
    [pickerData release];
    [super dealloc];
}
@end

gary

+2  A: 

No, viewDidUnload: is called when a UIViewController's view is released. dealloc: is only called when the UIViewController's reference count goes to zero. The author's code is good practice.

The author is using synthesized methods to set the ivars to nil, which means those ivars are sent release messages. viewDidUnload: is where you're supposed to release any objects or memory you can easily recreate. The author is essentially saying, "I don't need references to these things anymore, decrement the retain count and hopefully that will free up some memory. I'll recreate it later if necessary in viewDidLoad:."

Setting the ivars to nil will have no consequences if dealloc is called as messages to nil are handled by the Objective-C runtime.

Rob Jones
When the UIViewControllers reference count goes to zero, i.e. when the UIViewController is released. I was not aware of that, good to know and a good reason have both [self setPickerData:nil];
fuzzygoat
Thanks for the extra info in the edit Rob, much appreciated.
fuzzygoat