viewWill(Appear/Disappear) XIB Objects allocation?


I got my challenge Working and I want to practice making the code memory efficient. So for the challenge in the whereamI we have

IBOutlet MKMapView *worldView; IBOutlet UIActivityIndicatorView *activityIndicator; IBOutlet UITextField *locationTitleField;

my ViewDidLoad() looks like this

[code]- (void)viewDidLoad
[super viewDidLoad];
// [[self view] setBackgroundColor:[UIColor purpleColor]];

NSLog(@"Loaded the view for MKMapViewController");
[super viewDidUnload];


my ViewDidUnLoad() looks like this

-(void)viewDidUnload { NSLog(@"Mapview Did unload due to memory"); [super viewDidUnload]; [activityIndicator release]; activityIndicator = nil; [worldView release]; worldView = nil; [locationTitleField release]; locationTitleField = nil; }

If i do the simulate memory warning the viewDidUnload method gets called and all the objects activityIndicator, worldView, locationTitleField should release and deallocate. So when I go back to the view, the view works but how is the worldview, activityindicator and locationtitlefield being allocated? I notice if i take out the =nil it crashes hard. I’m just curious how are those objects allocating back?


Hi Devastadus,

it looks to me like the UIViewController’s loadView method creates the objects based on the associated nib file, and sets up the IBOutlet links for the owner (your mapView object).

I think the code that deals with loading/unloading views based on nib data assumes all the IBOutlets are set up like properties with retain. So if your worldView variable isn’t nil, it get’s sent a release message first, before being set to the new UIView object. If worldView was in fact deallocated prior to this, you get a crash trying to deallocate a non-existent object. That’s my interpretation, which I am not sure about and only write since no one else has replied to your post yet… Hopefully this will become clearer for us in later chapters.

BTW, why do you send [super viewDidUnload] in viewDidLoad ?