开发者

UIViewController view being unloaded even when visible

I have a view controller A being shown modally over another view controller B within a navigation controller container. When simulating memory warnings when this view controller A is shown modally, I receive the didReceiveMemoryWarnings message as expected, and then I receive a message viewDidUnload, 开发者_如何学运维thereby making my view controller A's view disappear.

This is surprising to me, and I am not sure how to resolve this. Why is viewDidUnload being called, releasing the visible view in the process?

Here is a stack trace in case it helps pinpoint the problem:

[UIViewController unloadViewForced:] ()

0x01458535 in -[UIViewController unloadViewIfReloadable] ()

0x01463eb8 in -[UINavigationController purgeMemoryForReason:] ()

0x01457b6d in -[UIViewController didReceiveMemoryWarning] ()

Thanks for any help!


According to http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html

Memory Management

Memory is a critical resource in iOS, and view controllers provide built-in support for reducing their memory footprint at critical times. The UIViewController class provides some automatic handling of low-memory conditions through its didReceiveMemoryWarning method, which releases unneeded memory. Prior to iOS 3.0, this method was the only way to release additional memory associated with your custom view controller class but in iOS 3.0 and later, the viewDidUnload method may be a more appropriate place for most needs.

When a low-memory warning occurs, the UIViewController class purges its views if it knows it can reload or recreate them again later. If this happens, it also calls the viewDidUnload method to give your code a chance to relinquish ownership of any objects that are associated with your view hierarchy, including objects loaded with the nib file, objects created in your viewDidLoad method, and objects created lazily at runtime and added to the view hierarchy. Typically, if your view controller contains outlets (properties or raw variables that contain the IBOutlet keyword), you should use the viewDidUnload method to relinquish ownership of those outlets or any other view-related data that you no longer need.

But I tried to do the same experiment on iphone simulator and the method viewDidUnload is not called. I think your behavior can take place when really low memory.


You can simulaute low memory warning from simulator by selecting Hardware>> Simulate memory warning.

And from IOS 6 on low memory viewDidUnload won't be called. So you need to manage what all memory can freed by overriding didreceivememory method warning.

It important to understand, after viewDidUnLoad, viewDidLoad again get called. So you need to assume viewDidLoad will get multiple times in ViewController life cycle.

Say you are not using arc, and allocating array, after some time due to low memory viewDidLoad again get called, this time you again create array than it will cause memory leaks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜