In my app I define some attributes in my h file.
As far as I understand, if there is an attributes that gets "alloc" someplace in the m file, I should release it in the dealloc method (or before if it's not relevant anymore).
I'm not clear about attributes that don't get "alloc":
- NSString - should I release them (I read posts that say "yes" and posts that say "no")
- NSDictionary / NSArray - when I create them I don't use "alloc", but some arrayWith or dictionaryWith methods; do they get memory allocation that require releasing or can I assume that XCode is taking care of their data management?
- BOOL - just want to verify that I don't need to release them
- int - just want to verify that I don't need to release them
- UI items (like UIButton, UILabel, etc...) - these items are connected in IB, I don't have an "alloc" from within the code - is IB releasing them or am I supposed to do it myself?
- How does the attribute being Propertied affects the memory management requirements? copy / retain / assign / nonatomic (I understand that when I开发者_如何转开发 do "copy" I'm creating another copy and therefore supposably allocation more memory, but I saw example codes that in some cases release copied propertied attributes and in some cases not)
I don't mind reading another tutorial (in most cases when I ask a question about Memory Management, I get people sending me the link to the Apple Memory Management Guide), but if someone can explain it or can direct to a more vivide/clear tutorial than the one Apple presents I would very much appreciate it.
@"some string"
is autoreleased
. Don't worry about these. When you pass it to an object, like viewController.title = @"some string";
the string will be retained and managed by the other object.
There are two ways to create NSSet
s, NSArray
s or NSDictionary
s.
You can either use alloc
and init
and have to release
it later on, or use the +arrayWith...
methods which return an autoreleased
object.
Don't worry about primitive data types like BOOL
s or int
s.
Properties which retain
the value are strong properties, basically they make sure that the objects stays in memory until they release
it again. You mostly use retain
for any kind of object, watch out for retain cycles though!
There are lots of answers out there for this question, as you say you have read the memory management tutorials, but I guess what you are looking for is a nice example to get it clear in your head.
The most helpful explanation on Cocoa memory management I read was in the Hillegass book (Cocoa Programming for Mac OS X), where he described an object as like a dog. Every time you use one of the methods that increases the retain count (new
, copy
,retain
,init
a few I've bound to have forgotten, these are in the memory management guide you've already read) then you are putting another lead on the dog's neck.
I will now take this perfectly good metaphor to silly extremes. You asked for a vivid example!
You have to take any of your leads off the dog once you are done with it. Otherwise you'll drive off with it tied to your car like on National Lampoon's Vacation, which isn't nice - this is a memory leak.
If you don't have a lead on the dog, you can't guarantee it will be there when you want it to do a trick. This could be sending a message to a released object.
If you go to take your lead off the dog, and there are no leads on it, then the dog could horribly savage you as you over-release it.
So, in essence, if you've put a lead on the dog, you have to take it off. If you haven't (i.e. you havent called new
or retain
etc) then you don't. This applies to all objects. BOOL
and int
are not objects so you don't have to worry about them.
No dogs are harmed during my day to day programming activities
精彩评论