I have a method that needs to return an array of objects. The way it does that now is:
- Create a
NSMutableArray* - Each object, following some computation, is
alloc-d andinit-ed - Each object, after initialization, is added to the array with
addObject - The array is returned
Is autoreleasing the array the right thing to do? What about the objects inside the array? When should these be released?
Yes, setting the array to
autoreleasebefore you return it is a reasonable thing to do. Also, if you are callingallocandiniton the things that you put into the array, you should callrelease(orautorelease) on each one after you add it to the array. Your objects will still be retained as long as they are in the array. Removing them from the array (or releasing the array) will cause them to be released.