Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MACloop

macrumors 6502
Original poster
May 18, 2009
393
0
Germany
Hello,
regarding memory management. I have read that if the (void)didReceiveMemoryWarning is called in case of a memory warning, the developer may/should release and set objects to nil in the (void)viewDidUnload method. I have seen different ideas about what and how you a shall do in this method. What I did not really understand is - will the viewDidLoad method be called after the view did unload? If so - shall I release AND/OR set all objects to nil created in the viewDidLoad method?
ie [object release]; object = nil;
What about the view self?

I have a question about the dealloc to. I release all the objects that have been retained or alloced in the code. Should I set some objects to nil here? What kind of objects? I have a navigation logic using some table views. Everytime the user goes back and/or forth the viewDidLoad method will be called...and as I understand, the also the dealloc in case the user leaves a tableview.

Any ideas?
Thanks in advance!
MACloop
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
viewDidUnload won't necessarily be called after didReceiveMemoryWarning. If the view controller is the frontmost view controller its view won't be unloaded.

In viewDidUnload you should use properties to release and set to nil your outlets:

self.myOutlet = nil;

You need to analyze your code for other variables but in most cases any ivar created in viewDidLoad needs to be released and set to nil in viewDidUnload. When the view is loaded again viewDidLoad will be called again. If you don't release the ivar in viewDidUnload you will have a memory leak.

In dealloc you need to release all your ivars but you don't need to set them to nil.
 

MACloop

macrumors 6502
Original poster
May 18, 2009
393
0
Germany
viewDidUnload won't necessarily be called after didReceiveMemoryWarning. If the view controller is the frontmost view controller its view won't be unloaded.

In viewDidUnload you should use properties to release and set to nil your outlets:

self.myOutlet = nil;

You need to analyze your code for other variables but in most cases any ivar created in viewDidLoad needs to be released and set to nil in viewDidUnload. When the view is loaded again viewDidLoad will be called again. If you don't release the ivar in viewDidUnload you will have a memory leak.

In dealloc you need to release all your ivars but you don't need to set them to nil.

Thanks alot! Your explanation is great! It really answered my questions!
MACloop
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.