My class has an NSArray that is filled with objects. In my dealloc method, can I simply call release on my NSArray, or do I need to iterate the array and release all objects first?
views:
3069answers:
3You can call release directly on the NSArray
. The implementation of NSArray
will take care of sending release
to all the objects stored in the array.
You should be able to just release the NSArray, and it will release all its objects, regardless of whether you're holding other references to them. If you have an instance object that also exists in the NSArray, you will have to release that object explicitly - just releasing the NSArray may not dealloc the object outside of the array context.
NSArray retains objects when they're added, and releases them when they're removed or the array is deallocated. Keep this in mind, it's this concept of "ownership" that retain/release memory management is built upon. It's the same with the object that owns the array, if it also retained the objects in the array you will need to send them another release message in your dealloc implementation. If not, and if no other objects retained them, they'll be deallocated once the array releases them.