zlog
zlog

Reputation: 3316

Is NSMutableArray recursive in releasing it's items?

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?

Upvotes: 1

Views: 520

Answers (2)

Richard
Richard

Reputation: 3386

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].

Upvotes: 1

Eiko
Eiko

Reputation: 25642

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.

Upvotes: 4

Related Questions