2

I have a method that needs to return an array of objects. The way it does that now is:

  1. Create a NSMutableArray*
  2. Each object, following some computation, is alloc-d and init-ed
  3. Each object, after initialization, is added to the array with addObject
  4. 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?

2 Answers 2

3

Yes, setting the array to autorelease before you return it is a reasonable thing to do. Also, if you are calling alloc and init on the things that you put into the array, you should call release (or autorelease) 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.

Sign up to request clarification or add additional context in comments.

1 Comment

To add to this correct answer the choice of whether to return an autoreleased NSArray is normally dependant on the name of your method. Following Apple's conventions if you use the words alloc, init or copy in the method name then you should not autorelease as the caller now expects that they own the object and are responsible for releasing it. If your method name does not contain these keywords then autoreleaseing is fine.
3

Your method should set the array to autorelease and then the caller should retain the returned array. So the method is no longer responsible for the array, the caller is.

The objects in the array will be retained by NSMutableArray, so you should set them to autorelease so they don't leak.

- (NSMutableArray*) calleeMethod
{
    // this method is retaining the array temporarily
    // someone else is responsible for retaining it
    NSMutableArray * newArray = [[[NSMutableArray alloc] init] autorelease];

    // add some objects
    for (int i = 0; i < 10; i++)
    {
        // autorelease these objects because newArray will retain each item and 
        // is responsible for the items
        FooObject * newFooObject = [[[FooObject alloc] initWithNumber:i] autorelease];
        [newArray addObject:newFooObject];
    }

    return newArray;
}

- (void) callerMethod
{
    // retain the returned array, because we own it 
    mNewArray = [[self calleeMethod] retain];

    // do stuff 

    // make sure you explicitly release mNewArray later (probably in the dealloc)
}

3 Comments

You don't need to set the objects in the array to autorelease. The containing array will retain them when added, and release them when they are removed. Just release them yourself after they are added to the array.
@highlycaffeinated If you alloc them, you will either need to explicitly release them or autorelease them.
Agreed. My (poorly made) point was there's little value in adding them to the autorelease pool if all you're doing is adding them to the array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.