1

Trying to hunt down the cause of crashing on certain devices. I am noticing that my view controllers are receiving didReceiveMemoryWarning, but NOT viewDidUnload. And according to Apple:

you would not use didReceiveMemoryWarning to release references to view objects, you might use it to release any view-related data structures that you did not already release in your viewDidUnload method. (The view objects themselves should always be released in the viewDidUnload method.)

So,

A: Why is viewDidUnload not called? I can't remove my view objects here if it is never called.

B: If I'm not supposed to remove my view objects in didReceiveMemoryWarning, where else would I do this?

C: Using ARC, should I still need to remove view objects, set arrays to nil, etc?

soleil
  • 12,133
  • 33
  • 112
  • 183
  • I find it unlikely your crash is caused by not removing views and such. You should check using Instruments that running out of memory is causing the crashes. – MaxGabriel Oct 09 '12 at 05:11
  • @MaxGabriel I am running out of memory. But one of my view controllers has a lot of images. So when I get a warning I can free up memory be removing them. – soleil Oct 09 '12 at 05:15

3 Answers3

2

As the other mentioned viewDidUnload: is deprecated in iOS 6. But as additional information you should know, that it is seldom necessary to unload a UIView since iOS 6 is doing its magic thingie in the background -it is destroying the bitmap layer of the backing CALayer of the view (which is by far the biggest "part" of a UIView). If the view is needed again iOS will call drawRect: where you compose your view and everything will be ok.

For more information read this great article of Joe Conway: ViewController lifecycle in iOS 6

Pfitz
  • 7,336
  • 4
  • 38
  • 51
1

viewDidUnload is deprecated in iOS6. You "can" remove views in didReceiveMemoryWarning if you think it is necessary, but it is left up to you.

This thread may help as well.

viewDidUnload no longer called in ios6

Community
  • 1
  • 1
barley
  • 4,403
  • 25
  • 28
  • holy crap, I didn't know this. Doesn't that make any app that was cleaning up views in viewDidUnload highly susceptible to crashing when getting memory warnings? – soleil Oct 09 '12 at 05:11
  • And if the view doesn't get unloaded, how does the view controller know to reload the view? I can remove the image views in didReceiveMemoryWarning, but now they are gone when I go back to that view controller – soleil Oct 09 '12 at 05:14
  • I am not hundred percent, but my understanding is that if you set `self.view = nil`, on next access to the view, `viewDidLoad` will be called. BTW, if you are building your subviews as `IBOutlet`, best practice seems to declare them as `weak` properties, so that when you set `self.view = nil` all the subviews will be released too. – barley Oct 09 '12 at 12:19
0

didReceiveMemoryWarning is specifically targeted not to the unloading of a view, but rather for the view controller to release objects which can easily be recreated (i.e. UIIamges and the like). You should not release objects in your view unless they can easily be recreated as necessary.

zachjs
  • 1,738
  • 1
  • 11
  • 22