When i have a NSMutableArray must i release all the objects also?
When i have a NSMutableArray with some custom cla开发者_运维问答ss objects, declared
NSMutableArray *list = [NSMutableArray new];
Must i also release all the objects in *list?
Putting an object reference into a NSMutableArray
will make the retain count for that object incremented, because the array retains every object.
When sending the release
message to the array, it also send the same message to all the objects it references.
So in case you didn't retain them over the array, you shouldn't do anything more.
No, when the array is released, all the objects in the array are sent a release message. So when the autorelease pool is released or drained, all the objects in the list will be released in this case.
if you add an object in your NSMutableArray
, it calls retain
on that object. When you release the array , it will call release on all of the objects that it has because it called retain
previously on the object when you added.
So you don't need to call release
explicitly on each object, One release
it enough on array instance.
To understand the fundamental rules of memory management that you must abide by, read “Memory Management Rules”.
精彩评论