elp
elp

Reputation: 8131

How to deallocate objects in NSMutableArray?


i have this Mutable Array:

NSMutableArray *points = [pgroute getPoints:self];

where [getPoint...] do this:

{
 NSMutableArray *normPoints = [[NSMutableArray alloc] init];
 [normPoints addObject:@""];
 [...]
 return normPoints;
}

now,
points is an array of objects, right?

is correct to release *points array in this way?

for (int i = 0; i < [points count]; i++) {
    [(NSString *)[points objectAtIndex:i] release];
}
[points release];

or it is another correct way?

Xcode compiler, with RUN_CLANG_STATIC_ANALYZER tell me there is an

Incorrect decrement of the reference count of an object that is not owned at this point by the caller

How can i resolve this?

thanks,
alberto.

Upvotes: 0

Views: 1477

Answers (1)

BoltClock
BoltClock

Reputation: 723448

If you want to empty the array, just do this:

[points removeAllObjects];

If you want to release the array, you can even skip that and release it right away:

[points release];

The array will handle releasing the objects on its own. Then again if you're only adding NSString literals (@"using this notation") to the array, they don't need to be released since they are constants. That's a different story of course; my point is that NSMutableArray will deal with releasing stuff where necessary for you.

Upvotes: 2

Related Questions