Narwe
Narwe

Reputation: 177

Releasing NSMutableArray not affecting the count of elements in the array

Could someone explain to me the following result?

//generate an array with 4 objects
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:
                         [NSNumber numberWithInt:1],
                         [NSNumber numberWithInt:2],
                         [NSNumber numberWithInt:3],
                         [NSNumber numberWithInt:4],
                         nil];
//release the array    
[array release];

//get a count of the number of elements in the array
int count = [array count]; <---  count returns 4

Should my count not be zero? Does 'release' not remove all elements from the array?

Upvotes: 3

Views: 44

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

The value of count is undefined, because after the last release accessing properties of the array is illegal: essentially, you are accessing a dangling pointer.

If you would like to clear out the array without invalidating it, use removeAllObjects method.

Upvotes: 6

Related Questions