开发者

How to deallocate objects in NSMutableArray?

开发者 https://www.devze.com 2023-01-31 02:30 出处:网络
i have this Mutable Array: NSMutableArray *points = [pgroute getPoints:self]; where [getPoint...] do this:

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开发者_Python百科 is not owned at this point by the caller

How can i resolve this?

thanks,

alberto.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消