21

I just installed the new version of Xcode/ios6. viewDidUnload is now depreciated.

In the apple doc,

viewDidUnload [...] Deprecated in iOS 6.0. Views are no longer purged under low-memory conditions and so this method is never called.

But numbers of apps are using this callback to release their properties, like :

- (void)viewDidUnload {
    [super viewDidUnload];

    self.recipientButton = nil;
    self.connectButton = nil;
    self.infoLabel = nil;
}

This was the best practice to release your IBOutlets.

So, first question:
What is going to happen these existing apps in iOS 6? Will they leak ?

and second one:
What is the new recommended way to release an IBOutlet property ? In dealloc method ?

Martin
  • 11,881
  • 6
  • 64
  • 110

3 Answers3

22

For the first Question:

Your ViewController will receive didReceiveMemoryWarning method callback and you can nil out the view & other components in this method

For Reference Do Check WWDC 2012 video Session on EVOLUTION OF VIEW CONTROLLER, in case you haven't (I Believe they are available only for registered developers, but not sure).

Answer to your second one.

[object release]; in dealloc. No need to assign nil to object before releasing.

Pranav Jaiswal
  • 3,752
  • 3
  • 32
  • 50
  • Thanks for your answer. In fact, i'm more interested to first question. But as http://stackoverflow.com/a/9281416/127493 says (for example, there are numbers of other topic on), IBOutlet properties should be released in viewDidUnload in previous best practice. – Martin Sep 20 '12 at 09:14
  • Of course, `[object release];` in dealloc is only possible if you are *not* running ARC. –  Oct 10 '12 at 13:52
  • 7
    Verbatim from the WWDC 2012 video on View Controllers: "viewWillUnload and viewDidUnload. We're not going to call them anymore. I mean, there's kind of a cost-benifit equation and analysis that we went through. In the early days, there was a real performance need for us to ensure that on memory warnings we unloaded views. There was all kinds of graphics and backing stores and so forth that would also get unloaded. We now unload those independently of the view, so it isn't that big of a deal for us for those to be unloaded, and there were so many bugs where there would be pointers into – Rose Perrone Jan 03 '13 at 01:18
  • 3
    unloaded views that didn't get cleared, that at the end of the day we didn't think it was worth automatically calling those. So we're gonna deprecate those methods. You can still get the same behavior. Your view controllers will still receive 'didReceiveMemoryWarning', and if they want to, they can nil out that view. It will work as expected. You might want to check first that your view isn't in the window before you nil it. I make that point because some applications actually use viewWillUnload, viewDidUnload as a means to get rid of other resources as well that weren't necessarily related to – Rose Perrone Jan 03 '13 at 01:19
  • 3
    the view, per se, so now that code might have to shift into didReceiveMemoryWarning." – Rose Perrone Jan 03 '13 at 01:20
  • I've found that in certain cases with iOS 6 that the backing stores (a view controller's view for example) are not being unloaded. A simple example would be pushing a new instance of a view controller onto a navigation controller repeatedly. With iOS 6 I've seen physical memory free run down to around 8MB, low memory alerts get fired, and the app doesn't recover any additional memory before eventually getting killed by the OS. One solution is to check to see if the current view controller's view is loaded and not visible on didReceiveMemoryWarning and unload it/set it to nil. – Ari Braginsky Feb 21 '13 at 23:33
  • Funny how IB still adds them to viewDidUnload when dragging connections between IB and source views. – Camsoft Mar 11 '13 at 11:55
9

I recommend you to use weak property for the IBOutlets like

@property (weak) IBOutlet UILabel * labelText;

That way you don't need to do anything in dealloc. In iOS 6, simply ViewDidUnload won't call, iOS5 or earlier it is just call when memory warning have occur.

Tomohisa Takaoka
  • 885
  • 5
  • 16
1

and second one: What is the new recommended way to release an IBOutlet property ? In dealloc method ?

What is the "old" recommended way? You must always release retained instance variables in dealloc; it has always been this way and continues to be this way.

It was just that in viewDidUnload (which is only called for low memory) you could also set your properties to nil.

user102008
  • 30,736
  • 10
  • 83
  • 104