ios - 致命异常 : NSRangeException - index 2 beyond bounds [0 .。 1]

标签 ios objective-c crash

在我在 swift 项目中使用的库之一中,一行导致应用程序崩溃。我试图理解并修复它,但没有运气。我知道它是由数组索引错误引起的。谁能帮忙?

崩溃报告

Fatal Exception: NSRangeException
0  CoreFoundation                 0x180a42e38 __exceptionPreprocess
1  libobjc.A.dylib                0x1800a7f80 objc_exception_throw
2  CoreFoundation                 0x180922ebc -[__NSArrayM removeObjectAtIndex:]
3                          0x10000ac70 -[ChatSectionManager messageForIndexPath:] (ChatSectionManager.m:435)
4                          0x10001c194 -[Chat tableView:cellForRowAtIndexPath:] (Chat.m:596)
5  UIKit                          0x185ee2f40 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:]
6  UIKit                          0x185ee30a8 -[UITableView _createPreparedCellForGlobalRow:willDisplay:]

ChatSectionManager.m

 - (QBChatMessage *)messageForIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.item == NSNotFound) {
        return nil;
    }

    QMChatSection *currentSection = self.chatSections[indexPath.section];
    //crashes here line 435
    return currentSection.messages[indexPath.item];
}

Chat.m

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.chatSectionManager messagesCountForSectionAtIndex:section];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.chatSectionManager.chatSectionsCount;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {

    QBChatMessage *messageItem = [self.chatSectionManager messageForIndexPath:indexPath];

... 方法包含 removeObjectAtIndex

- (void)deleteMessages:(NSArray *)messages animated:(BOOL)animated {

    dispatch_async(_serialQueue, ^{

        NSMutableArray *messagesIDs = [NSMutableArray array];
        NSMutableArray *itemsIndexPaths = [NSMutableArray array];
        NSMutableIndexSet *sectionsIndexSet = [NSMutableIndexSet indexSet];

        self.editableSections = self.chatSections.mutableCopy;

        for (QBChatMessage *message in messages) {
            NSIndexPath *indexPath = [self indexPathForMessage:message];
            if (indexPath == nil) continue;

            QMChatSection *chatSection = self.chatSections[indexPath.section];
            [chatSection.messages removeObjectAtIndex:indexPath.item];

            if (chatSection.isEmpty) {
                [sectionsIndexSet addIndex:indexPath.section];
                [self.editableSections removeObjectAtIndex:indexPath.section];

                // no need to remove elements whose section will be removed
                NSArray *items = [itemsIndexPaths copy];
                for (NSIndexPath *index in items) {
                    if (index.section == indexPath.section) {
                        [itemsIndexPaths removeObject:index];
                    }
                }
            } else {

                [itemsIndexPaths addObject:indexPath];
            }
        }

        dispatch_sync(dispatch_get_main_queue(), ^{

            self.chatSections = self.editableSections.copy;
            self.editableSections = nil;

            if ([self.delegate respondsToSelector:@selector(chatSectionManager:didDeleteMessagesWithIDs:atIndexPaths:withSectionsIndexSet:animated:)]) {

                [self.delegate chatSectionManager:self didDeleteMessagesWithIDs:messagesIDs atIndexPaths:itemsIndexPaths withSectionsIndexSet:sectionsIndexSet animated:animated];
            }
        });
    });
}

注意:这个崩溃是随机发生的,我不知道为什么

织物损坏报告 http://crashes.to/s/679e90f0c90

最佳答案

肯定是deleteMessages的问题,看看你的16号线程哪里崩溃了:

QMChatSectionManager.m line 228 __48-[QMChatSectionManager deleteMessages:animated:]_block_invoke

检查第 228 行到底是哪一行可以确定问题出在哪里,但我会冒险猜测是什么

[chatSection.messages removeObjectAtIndex:indexPath.item];

所以您要在线程 16 上删除 messageForIndexPath 在主线程上引用的数据结构。正如您所注意到的,这是对竞争条件的公开邀请,几乎不可能按需重现。

解决方法是不在后台线程中修改chatSection 的内容。看起来你已经有了正确的想法,在主线程上分配完成的部分列表,

self.chatSections = self.editableSections.copy;

只需将该逻辑应用到部分列表中更改的每个项目,您的随机崩溃就会消失。

关于ios - 致命异常 : NSRangeException - index 2 beyond bounds [0 .。 1],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37150746/

相关文章:

ios - 从 Swift 函数中的异步调用返回数据

ios - 如何在 iOS 中将一个 Xcode 项目使用到另一个 Xcode 项目中?

ios - 没有为 iPhone 5s 编译的架构

ios - 由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序,原因 : '-[__NSCFArray length]: unrecognized selector sent to instance

xcode - 为什么我的iPhone模拟器的应用程序不断崩溃?

ios - 将配置更改推送到 iOS 应用程序

ios - 将矩形图像比例缩放为适合正方形的比例,以与MPMediaItemArtwork一起使用

iphone - 使用 IF 语句将对象从 NSMutableArray 复制到另一个

ios - 根据 UILabel 中的数字动态更改 UIView 宽度

bash - exp_close 和 exp_wait 之后预计脚本崩溃