Trying to understand memory-management concepts...

Discussion in 'iOS Programming' started by jshmrsn, Jul 28, 2008.

  1. macrumors member

    Joined:
    Jul 26, 2008
    #1
    So, as my app grows it is gaining a considerable number of views. I've heard when you switch between views, you should discard the memory of the old ones. How do I got about doing this?

    If I [release myView] it seems to always crash. Are there any examples of this?

    Thanks
     
  2. macrumors member

    Joined:
    Jul 9, 2008
    #2
    Releasing views

    I am also new to Mac programming, so someone else jump in if I'm wrong. ;)

    I believe that views are retained when they are added as subviews and released with the superview is finished with them. So you don't need to release them explicitly, unless you've retained them somewhere.

    But the memory management issue is to be sure you clean up any resources that the view holds. I don't know if this is the commonly accepted pattern, but I ensure that everything that was allocated by the view (and not released immediately) is released in the dealloc method of the view.

    Then, of course, there is just the normal cascading deallocation. Ensure that all of your classes implement dealloc and are releasing their resources when they are released (if they can't release them sooner).

    So when the view is deallocated, it releases any resources that it is responsible for but hasn't yet released, those resources then release anything they were retaining, etc.

    Again, I'm new, but when I run Leaks against my app, I'm not leaking much in the way of resources. (I do have a couple small leaks to track down. :()
     
  3. thread starter macrumors member

    Joined:
    Jul 26, 2008
    #3
    Hm okay, thanks.
    I guess part of my problem is that I do retain them in most cases. I'll try to work on the design so that isn't needed as much.
     
  4. macrumors 6502

    Joined:
    Jun 26, 2008
    #4
    Rule of thumb that should help: if the function starts with init it's been retained for you, if it does not you need to retain it if you want to keep it because it's autoreleased. If you're subclassing UIViewController or one of it's subclasses, they handle discarding invisible views for you.
     

Share This Page