NSMutableArray removeAllObjects 导致崩溃

发布于 2024-12-11 04:20:38 字数 634 浏览 0 评论 0原文

我有几个数组,我试图从中清除所有对象,但使用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 技术交流群。

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

发布评论

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

评论(2

等你爱我 2024-12-18 04:20:38

这很可能是因为您没有正确管理数组包含的对象之一的内存。当您从数组中删除对象时,其保留计数会减少一次。

您可以在清除数组的行上放置一个断点,并使用调试器查看其中哪个对象无效。

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.

鸠魁 2024-12-18 04:20:38
ballArray = [[NSMutableArray alloc] init];

ballVelocityArray = [[NSMutableArray alloc] init];

分配对象后,您还可以在 dealloc 函数或其他地方释放该对象,请检查这一点。如果您这样做,那么我想通知您,

[ballArray removeAllObjects];

[ballVelocityArray removeAllObjects];

removeAllObjects 不仅会删除数组的所有对象,还会从内存中释放数组对象,所以如果再次释放数组对象时,内存指针将到达 -1,应用程序将崩溃。

因此,请确保如果您已经在使用函数,则不必释放数组对象

[ballArray removeAllObjects];

[ballVelocityArray removeAllObjects];

ballArray = [[NSMutableArray alloc] init];

ballVelocityArray = [[NSMutableArray alloc] init];

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

[ballArray removeAllObjects];

[ballVelocityArray removeAllObjects];

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

[ballArray removeAllObjects];

[ballVelocityArray removeAllObjects];

functions.

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