Trying to understand memory-management concepts...

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

  1. jshmrsn macrumors member

    Jul 26, 2008
    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?

  2. caldwelljason macrumors member

    Jul 9, 2008
    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. jshmrsn thread starter macrumors member

    Jul 26, 2008
    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. ayasin macrumors 6502

    Jun 26, 2008
    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