user4951
user4951

Reputation: 33050

Why do we need both (void)viewDidUnload and (void)dealloc {

When is a view unloaded and yet not deallocated?

Why do in viewDidUnload we do

self.member = nil;

and in dealloc we do

[member release]

They do almost the same thing but why we do one in viewDidUnload and the other in dealloc?

Upvotes: 1

Views: 669

Answers (3)

Fuggly
Fuggly

Reputation: 915

viewDidUnload is called when the view is unloaded (hence the name) and is used to unload basically everything you load with loadView oder viewDidLoad to free up memory when memory resources are scarse. The system gives you an opportunity to free up resources that can later be loaded again with viewDidLoad / loadView. Usually, you would free up every visual components or hierarchies you created in viewDidLoad or over the NIB. But you would retain your data and free only resources that cannot get automatically (viewidLoad, loadView or any of your own caching mechanisms) reloaded. In dealloc you release everything that is retained by your class

Upvotes: 0

GoatInTheMachine
GoatInTheMachine

Reputation: 3773

I don't think you should be doing that, as member will be nil when you send the release message in your dealloc method, so the original member will leak.

Upvotes: 0

Nyx0uf
Nyx0uf

Reputation: 4649

viewDidUnload get called when your app receive a memory warning. You have to release all retained views that can be reconstructed in loadView or viewDidLoad.

Upvotes: 1

Related Questions