如何释放 NSMutableArray 中的对象?
我有这个可变数组:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你想清空数组,只需这样做:
如果你想释放数组,你甚至可以跳过它并立即释放它:
数组将自行处理释放对象。再说一遍,如果您仅将
NSString
文字(@"using this notation"
)添加到数组中,则不需要释放它们,因为它们是常量。当然,这是一个不同的故事;我的观点是,NSMutableArray
将在您需要时处理释放内容。If you want to empty the array, just do this:
If you want to release the array, you can even skip that and release it right away:
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 thatNSMutableArray
will deal with releasing stuff where necessary for you.