OStack程序员社区-中国程序员成长平台

标题: ios - __NSArrayM objectAtIndex 崩溃,但我进行了验证 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 01:01
标题: ios - __NSArrayM objectAtIndex 崩溃,但我进行了验证

我遇到了这个崩溃:

Last Exception Backtrace:
0   CoreFoundation                  0x1838551b8 __exceptionPreprocess + 124 (NSException.m:165)
1   libobjc.A.dylib                 0x18228c55c objc_exception_throw + 56 (objc-exception.mm:521)
2   CoreFoundation                  0x18373071c -[__NSArrayM objectAtIndex:] + 228 (NSArray.m:389)
3   living                          0x100c0d89c -[RequestCache sendEvents] + 76
4   libdispatch.dylib               0x1826de1bc _dispatch_client_callout + 16 (object.m:455)
5   libdispatch.dylib               0x1826eaf94 _dispatch_continuation_pop + 576 (inline_internal.h:2424)
6   libdispatch.dylib               0x1826f7634 _dispatch_source_latch_and_call + 204 (source.c:594)
7   libdispatch.dylib               0x1826e0160 _dispatch_source_invoke + 820 (source.c:897)
8   libdispatch.dylib               0x1826e2bbc _dispatch_main_queue_callback_4CF + 572 (inline_internal.h:2461)
9   CoreFoundation                  0x183802810 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12 (CFRunLoop.c:1793)
10  CoreFoundation                  0x1838003fc __CFRunLoopRun + 1660 (CFRunLoop.c:3004)
11  CoreFoundation                  0x18372e2b8 CFRunLoopRunSpecific + 444 (CFRunLoop.c:3113)
12  GraphicsServices                0x1851e2198 GSEventRunModal + 180 (GSEvent.c:2245)
13  UIKit                           0x1897757fc -[UIApplication _run] + 684 (UIApplication.m:2650)
14  UIKit                           0x189770534 UIApplicationMain + 208 (UIApplication.m:4092)
15  living                          0x10065fb2c main + 88 (main.m:15)
16  libdyld.dylib                   0x1827115b8 start + 4

崩溃发生在以下代码中:

NSMutableArray * cachedRequests = [[NSMutableArray alloc] init];

   // ....

- (void) sendEvents {

    if (cachedRequests != nil && [cachedRequests count] == 0){
        return;
    }
    MyCacheData *requestData = [cachedRequests objectAtIndex:0]; // <- Crash happens here

    if (requestData != nil) {
        [cachedRequests removeObject:requestData];
    }    
}

我从不同的地方和不同的线程调用sendEvents,例如:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self sendEvents];
    });

或来自 NSURLSessionDataTask

的回调

我需要用 @synchronized (self) {} 锁定所有方法内容吗?

喜欢:

- (void) sendEvents {
 @synchronized (self) {   
    if (cachedRequests != nil && [cachedRequests count] == 0){
        return;
    }
    MyCacheData *requestData = [cachedRequests objectAtIndex:0]; 

    if (requestData != nil) {
        [cachedRequests removeObject:requestData];
    }  
  }  
}

或者有其他更合适的方法来摆脱这个崩溃?


我从生产版本的 iTunes Connect 中得到了这个异常

[编辑]

当我尝试为 cachedRequests 设置 0 长度并调用 [cachedRequests objectAtIndex:0] 我得到 Exception:

* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'

但是从我的崩溃报告中我看到:

-[__NSArrayM objectAtIndex:] + 228 (NSArray.m:389)

两者是否相同或由于多线程而出现问题?



Best Answer-推荐答案


我认为您的解决方案看起来很明智,这似乎是一个并发问题。

根据您类(class)的其余部分发生的情况,我将使用 cachedRequests 对象作为同步 @synchronized (cachedRequests) 的 token 。取决于您是否有其他想要与同一个锁同步的东西?

您还需要将具有相同 token 的相同锁添加到任何其他操作此数组的代码,以同步对它的所有访问。

关于ios - __NSArrayM objectAtIndex 崩溃,但我进行了验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43386469/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4