如何释放 NSMutableArray 中的对象?

发布于 2024-10-08 02:09:37 字数 695 浏览 2 评论 0原文


我有这个可变数组:

NSMutableArray *points = [pgroute getPoints:self];

其中[getPoint...]执行此操作:

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

现在,
点是一个对象数组,对吗?

以这种方式释放 *points 数组是否正确?

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

或者这是另一种正确的方法?

Xcode 编译器,使用 RUN_CLANG_STATIC_ANALYZER 告诉我有一个

引用的递减不正确 不被拥有的对象的计数 此时调用者

我该如何解决这个问题?

谢谢,
阿尔贝托.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

定格我的天空 2024-10-15 02:09:37

如果你想清空数组,只需这样做:

[points removeAllObjects];

如果你想释放数组,你甚至可以跳过它并立即释放它:

[points release];

数组将自行处理释放对象。再说一遍,如果您仅将 NSString 文字(@"using this notation")添加到数组中,则不需要释放它们,因为它们是常量。当然,这是一个不同的故事;我的观点是,NSMutableArray 将在您需要时处理释放内容。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文