2

I am new to iPhone development.

I am using some buttons and labels in my app. I am releasing all these buttons and labels in dealloc function.
I am also releasing them in viewDidUnload.

- (void)viewDidUnload
{
[super viewDidUnload];
self.ans1 = nil;
self.ans2 = nil;
self.ans3 = nil;
self.ans4 = nil;
self.button = nil;
self.button2 = nil;
self.button3 = nil;
self.button4 = nil;
}

I just want to know that it is good for memory management or it makes no difference to memory management.
And why we use that??

Thanks in advance..!!

iUser
  • 1,075
  • 3
  • 20
  • 49

3 Answers3

3

it is not the view controller that is unloaded when viewDidUnload is called but only its view. The view controller stays alive until it is deallocated.

In viewDidUnload, you have to release those objects that are part of the view and everything that can and will be recreated in viewDidLoad (because viewDidLoad will be called again when the view controller need to recreate its view). This includes all your outlets. You also have to set these variables to nil to avoid overreleasing them.

Because in dealloc, you should release all objects your view controller retains, including those you included in viewDidUnload.

PJR
  • 13,052
  • 13
  • 64
  • 104
  • So, If i use UIImageView , NSInteger , NSArray and UIScrollView then I have to make them nil in viewDidUnload ?? – iUser Aug 17 '11 at 09:00
  • in general practice you can do it all in dealloc method , as you see in most of the apps. but regarding to this question you can release iamgeView, scrollview in viewDidUnload and set then nil. – PJR Aug 17 '11 at 09:04
  • Thanks !! but can i release these objects in ViewDidUnload and also in dealloc function ?? or it causes any memory leak?? – iUser Aug 17 '11 at 09:09
  • in viewDidUnload if you release any variable and set it to "nil" and then again release in dealloc then "Nothing happen"...BUT if you release any variable and "not set to nil" and again release in dealloc then "app will crash". – PJR Aug 17 '11 at 09:16
2

viewDidUnload is used to release anything that you might have made when the view is created - this included things in viewDidLoad but also includes and IBOutlet properties that are created from inside a xib file. these should all be released and set to nil in viewDidUnload.

Anything else should just be released in dealloc.

The idea is that if viewDidUnload is called to free some memory, the view can be recreated again completely from your viewDidLoad method.

Also see this question;

memory management on the iPhone with viewDidUnload

Community
  • 1
  • 1
1

It is good for memory management. If you release objects associated with a View controller when the controller unloads, you effectively reduce the memory footprint of your application. Retaining objects even when you are not using them makes your application more prone to memory warnings & eventual termination.

Hence, it is good practice to allocate in ViewDidLoad & release in ViewDidUnload.

HTH,

Akshay

Akshay
  • 5,747
  • 3
  • 23
  • 35