With NSArray object references, do I explicitly free all objects in the array, or only the array itself?

In my class there is an NSArray filled with objects. In my dealloc method, can I just call release on my NSArray or do I need to iterate through the array and release all objects first?

+44
memory-management objective-c iphone
May 20 '09 at 1:59 a.m.
source share
3 answers

You can call release directly on NSArray . An NSArray implementation NSArray take care of passing release to all objects stored in the array.

+55
May 20 '09 at 2:01 a.m.
source share

NSArray saves objects when they are added and frees them when they are deleted, or the array is freed. Keep this in mind, based on this concept of โ€œownershipโ€ that preserves / frees memory management. Same thing with the object that owns the array, if it also saved 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 have saved them, they will be freed after the array frees them.

+10
May 20 '09 at 2:43 a.m.
source share

You just have to free NSArray and it will release all its objects, regardless of whether you hold other references to them. If you have an instance object that also exists in NSArray, you will need to release that object explicitly - only releasing NSArray cannot disassemble the object outside the context of the array.

+1
May 20 '09 at 2:02
source share



All Articles