View Full Version : Trying to understand memory-management concepts...
Jul 29, 2008, 01:42 AM
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?
Jul 30, 2008, 05:06 PM
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. :()
Jul 30, 2008, 06:05 PM
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.
Jul 30, 2008, 11:29 PM
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.