NSMutableArray removeAllObjects 导致崩溃
我有几个数组,我试图从中清除所有对象,但使用removeAllObjects 会使应用程序崩溃并返回sigabrt。在我的研究过程中,我发现虽然我正在创建 NSMutableArrays,但我可以创建 NSArray 的实例,但我不确定我是否正在这样做......这是我对数组所做的一切
ballArray = [[NSMutableArray alloc] init];
ballVelocityArray = [[NSMutableArray alloc] init];
[ballArray addObject:MyUIImageView];
[ballVelocityArray addObject:[NSValue valueWithCGPoint:myCGPoint]];
[ballVelocityArray replaceObjectAtIndex:SomeIndex withObject:[NSValue valueWithCGPoint:NewVelocity]];
[ballArray removeAllObjects];
[ballVelocityArray removeAllObjects];
这就是我的一切已经完成了,我不明白为什么它不断崩溃......如果数组中只有一个对象,它工作正常,否则它会崩溃
任何帮助将不胜感激!
I have a couple of arrays that I am trying to clear all objects from, but using removeAllObjects crashes the app and returns sigabrt. During my research I've found that although I am creating NSMutableArrays I could be creating an instance of a NSArray, but I am not sure if I am doing this or not... Here is everything I do to the arrays
ballArray = [[NSMutableArray alloc] init];
ballVelocityArray = [[NSMutableArray alloc] init];
[ballArray addObject:MyUIImageView];
[ballVelocityArray addObject:[NSValue valueWithCGPoint:myCGPoint]];
[ballVelocityArray replaceObjectAtIndex:SomeIndex withObject:[NSValue valueWithCGPoint:NewVelocity]];
[ballArray removeAllObjects];
[ballVelocityArray removeAllObjects];
That is everything I have done and I can't figure out why it keeps crashing... if there is only one object in the arrays it works fine, otherwise it crashes
Any help would be greatly appreciated!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这很可能是因为您没有正确管理数组包含的对象之一的内存。当您从数组中删除对象时,其保留计数会减少一次。
您可以在清除数组的行上放置一个断点,并使用调试器查看其中哪个对象无效。
It's most likely because you are not managing memory correctly on one of the objects the array contains. When you remove an object from an array its retain count is decremented once.
You can put a break point on the line where you clear the array and use the debugger to see which object in there is invalid.
分配对象后,您还可以在 dealloc 函数或其他地方释放该对象,请检查这一点。如果您这样做,那么我想通知您,
removeAllObjects 不仅会删除数组的所有对象,还会从内存中释放数组对象,所以如果再次释放数组对象时,内存指针将到达 -1,应用程序将崩溃。
因此,请确保如果您已经在使用函数,则不必释放数组对象
。
After allocating object you are also releasing the object in dealloc function or somewhere else check this.If you are doing so then I would like to inform you that
removeAllObjects is not only removing all objects of array but also release the array object from memory so if again you are releasing the array object the memory pointer will reach on -1 and the application will crash.
So, make sure that You have not to release array object it you are already using
functions.