简体   繁体   English

NSMutableArray removeObject中的奇怪错误

[英]Strange bug in NSMutableArray removeObject

I have this snippet: 我有这个片段:

1: if ((s >= kEnumValue1) && (s <= kEnumValue2)) {
2:   MyObject * o = [self findObjectFor:s];
3:   if ([o isValidFor:self]) {
4:     [arrayOfMyObjects removeObject:o];
5:     for (MyObject * mo in arrayOfMyObjects) {
6:       ...
7:     }
8:   }
9: }

For some really weird reason, when my code reaches line 4, if I step over, it goes back to the start of the method on the same thread, without going thru lines 5-. 出于某些非常奇怪的原因,当我的代码到达第4行时,如果我跨步了,它将回到同一线程上方法的开始,而无需经过第5行。 Does anybody have any clue why this happens? 有人知道为什么会发生这种情况吗?

removeObject on NSMutableArray does not mention any exception. NSMutableArray上的removeObject没有提到任何异常。

It is safe to assume that you did not find a bug in removeObject: (unless you wrote your own). 它是安全的假设,你没有找到一个错误removeObject:除非你写你自己的)。

Have you checked arrayOfMyObjects ? 您检查过arrayOfMyObjects吗? Is it non-nil? 非零吗? Does it contain anything? 是否包含任何内容?

OK, after lots of debugging in all classes on my project, I found that, when MyObject's release is called, it calls "release" on another class (since it is the last reference, it calls "dealloc"), once again for a third class, and then, it invokes removeObjectForKey:nil in NSMutableDictionary. 好的,在项目的所有类中进行了大量调试之后,我发现,当调用MyObject的release时,它将在另一个类上调用“ release”(因为它是最后一个引用,因此会调用“ dealloc”),再次针对第三类,然后在NSMutableDictionary中调用removeObjectForKey:nil。 This raises an NSInvalidArgumentException that is not logged at all. 这将引发一个根本未记录的NSInvalidArgumentException。 Way too evil... 太邪恶了...

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

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