When I'm debugging my iPad app, I found because of low memory, some view controllers' viewDidUnload
got called. But seconds later, their viewDidLoad
are called. And then again because of low memory, viewDidUnload
, then viewDidLoad
again. This is like file system keep swapping files due to low memory.
Is it supposed to be like this, or I have done something wrong?
Then I wanna release the view controller to get rid of this. But sometimes viewDidUnload
is called before dealloc
, and then crash due to selector sent to deallocated view controller.
Thank you for any help.
This is perfectly normal behaviour.
viewDidUnload
is called in a low memory situation to notify your controller that the view has been released.There's a clear outline of the steps in the View Controller Programming Guide for iOS.
Specially, look at the steps detailed in the section Understanding How Views Are Loaded and Unloaded, where it covers the unload cycle:
and so on.
In your specific case, you definitely don't want to release the view controller in this case. If there's any further memory management you want to do whilst there's low memory, you should override the default implementation of didReceiveMemoryWarning. As the docs state, don't forget to call
[super didReceiveMemoryWarning];
.