Question

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..!!

Was it helpful?

Solution

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.

OTHER TIPS

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top