(void)didReceiveMemoryWarning and releasing

Discussion in 'iOS Programming' started by MACloop, Mar 24, 2010.

  1. MACloop macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #1
    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
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    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.
     
  3. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #3
    Thanks alot! Your explanation is great! It really answered my questions!
    MACloop
     

Share This Page