简体   繁体   English

NSMutableArray奇怪的崩溃-removeObjectAtIndex

[英]NSMutableArray strange crash - removeObjectAtIndex

I received a crash from one of my app which appears weird with following logs: 我从我的一个应用收到崩溃,该崩溃看上去很奇怪,并显示以下日志:

* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM removeObjectAtIndex:]: index 4294967293 beyond bounds [0 .. 4294967293]' *由于未捕获的异常'NSRangeException'而终止应用程序,原因:'* -[__ NSArrayM removeObjectAtIndex:]:索引4294967293超出范围[0 .. 4294967293]'

As per stack trace this is coming from below piece of code: 根据堆栈跟踪,这来自下面的代码:

if ([self.itemList count] > 0) {
    [self.itemList removeAllObjects];
}

Per my understanding, removeAllObjects is always a safe operation and should not end up in a crash like this. 据我了解, removeAllObjects始终是安全的操作,不应以这种崩溃而告终。 Am I missing something over here. 我在这想念什么吗? Any guidance to right direction would be really helpful. 任何正确方向的指导都将非常有帮助。

EDIT : 编辑

Here is my itemList property declared: 这是我声明的itemList属性:

@property (nonatomic, strong) NSMutableArray *itemList;

It is most likely caused by the property being non-atomic. 这很可能是由于非原子性质引起的。 As you mentioned this code is multi-threaded and you are using a property which is not thread safe. 正如您提到的,此代码是多线程的,并且您使用的是线程安全的属性。

  • Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM removeObjectAtIndex:]: index 4294967293 beyond bounds [0 .. 4294967293]' 由于未捕获的异常'NSRangeException'而终止应用程序,原因:'*-[__ NSArrayM removeObjectAtIndex:]:索引4294967293超出范围[0 .. 4294967293]'

This error simply means that the object that has to be deleted is already deleted and hence the returned index is '4294967293.' 该错误仅表示必须删除的对象已被删除,因此返回的索引为“ 4294967293”。 Try to set the array to atomic. 尝试将数组设置为原子。 Also try using @synchronized as mentioned in a similar question here: 也尝试使用@synchronized,如此处类似问题中所述:

NSMutableArray removeAllObjects beyond bounds exception NSMutableArray removeAllObjects超出范围异常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM