1

Should I release my subviews of UIView in the viewDidUnload when I have references to them as instance variables which retains them? I have build the GUI programmatically. I should do that right? Since both uiview and ivars retain then the objects would have 2 in retain-count, when view receives e.g. memory-warning then the UIView will release the subviews, but they still have +1 in retain count so I have to setself.myIvar = nil; In the viewDidUnload?

Thanks for your time.

user626912
  • 2,546
  • 3
  • 24
  • 33
  • 2
    I guess that's why apple put this comment in the viewDidUnload method of the template `// Release any retained subviews of the main view. // e.g. self.myOutlet = nil;` – Matthias Bauch Feb 22 '11 at 18:44
  • @fluchtpunkt can you elaborate on your comment and post it as an answer? When are they supposed to be re-initialized? – fearmint Feb 22 '11 at 18:51
  • Duplicate? http://stackoverflow.com/questions/1158788/when-should-i-release-objects-in-voidviewdidunload-rather-than-in-dealloc And, to not be a jerk that just shouts duplicate! In most cases - you should release (set to nil) all retained (probably as properties) views set up with either viewDidLoad, loadView or using interface builder outlets. Just don't erase any important data there. – Mayjak Feb 22 '11 at 19:37

1 Answers1

1

You actually can release all retained subviews in viewDidUnload. But I used to do it in another way:

-(void) viewDidLoad {
    someInstanceView1 = [[UIView alloc] init];
    [self.view addSubview: someInstanceView1];
    [someInstanceView1 release];

    someInstanceView2 = [[UIView alloc] init];
    [self.view addSubview: someInstanceView2];
    [someInstanceView2 release];

    //etc...
    //you have a references to someInstanceView1 and someInstanceView2 with retained counts 1
}

In this case even if memory warning will arise, the view controller will remove all it's view subviews. And then call viewDidLoad again. So there would be no leaks and you don't need to care about releasing that ivars at all cause the only owner (it has the strong reference to the views) is the view controller's view and it will release them automatically.

Max
  • 16,679
  • 4
  • 44
  • 57
  • 1
    But some subviews you need a reference to..then this won't work. – user626912 Feb 22 '11 at 19:00
  • What do you mean you need a reference to? v1 = [[UIView alloc] init]; - you have the one. And don't tell me it won't work: it works for about 10 projects ;) – Max Feb 22 '11 at 19:01
  • A reference as in an instance variable of that object. That way it can be accessed and modified later, extremely easier. How do you get rid of all those subviews: Cycle through the list of subviews and release them? With instance variables all you have to do is release them directly. This is standard practice and the way Apple does it (hint hint.) – fearmint Feb 22 '11 at 19:17
  • I tell you a little secret: [someInstanceView removeFromSuperview] - that's how you can remove that view (and it would be deallocated since retain count was 1). See edit in answer if v1 and v2 is not obvious to you. – Max Feb 22 '11 at 19:22
  • Just add IBOutlet's. It would be absolutely the same (in first case you add views manually and in IB case it does it automatically). You also don't need to release views manually. – Max Feb 22 '11 at 21:04