Take the 2-minute tour ×

If I have a 2 dimensional NSMutableArray eg,

board = [[NSMutableArray alloc] initWithCapacity:boardHeight];
for (int y = 0; y < boardHeight; y++) {
    NSMutableArray *row = [[NSMutableArray alloc] initWithCapacity:boardWidth];

    for (int x = 0; x < boardWidth; x++) {
        [row insertObject:@"A string"];             

    [board insertObject:row atIndex:y];
    [row release];

and I do

[board release];

Does that recursively release the array? Or do I have to manually go into the array and release each row?

If it does, and the object inserted into each row were a custom object, is there anything special I have to think about when writing the dealloc method for the custom object?

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

Everything will just work fine. The array will retain the objects when they are added and released when removed or the array is deallocated.

No extra work on that part.

share|improve this answer
add comment

When board is ready to be deallocated, it will send the release message to each object in itself. Even if those objects are NSArrays, they will still be released; and if those arrays are now ready to be deallocated, they will send release to their members, etc.

Your class should implement dealloc normally -- release any ivars that you hold a strong reference to before calling [super dealloc].

share|improve this answer
add comment

Your Answer


