Fasid
Fasid

Reputation: 255

Release any retained subviews of the main view on iPhone

I saw this code in the project template and a few other sample projects.

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

Can someone explain to me what self.myOutlet = nil does? Does it release the memory? I thought you put [myOutlet release] in - (void)dealloc to release the memory. what is = nil? and when do you need to do this?

Upvotes: 1

Views: 792

Answers (2)

Vladimir
Vladimir

Reputation: 170839

Typically viewDidUnload method is called when low memory warning occurred and controller's view gets unloaded (controller itself stays in memory). As David pointed in his answer usually outlets are being retained by controller so they stay in memory even after the main view is gone - that reduces the benefits of unloading the view.

You still need to release you outlets in dealloc method even if you release them in viewDidUnload

For more details see this SO question

Upvotes: 2

David
David

Reputation: 2871

If myOutlet is specified as a @property (retain) then whenever you assign it to point to a new object, the old one will be released and the new one retained. When you assign it to nil, that therefore releases the object that it previously pointed to.

Upvotes: 2

Related Questions