iOS崩溃复制NSArray EXC_BAD_ACCESS KERN_PROTECTION_FAILURE

时间:2017-07-12 20:08:21

标签: ios objective-c memory crash crashlytics

以下是来自Crashlytics的两个堆栈跟踪,两者都包含我的代码的相同行,但导致两个不同的崩溃。

# OS Version: 10.3.2 (14F90)
# Device: iPad 5
# RAM Free: 3.8%
# Disk Free: 90.7%

#0. Crashed: com.apple.main-thread
0  libsystem_platform.dylib       0x18d365090 _platform_memset + 126
1  libsystem_malloc.dylib         0x18d2ebd00 _nano_malloc_check_clear + 584
2  libsystem_malloc.dylib         0x18d2eacb0 nano_calloc + 80
3  libsystem_malloc.dylib         0x18d2dc4e8 malloc_zone_calloc + 168
4  libsystem_malloc.dylib         0x18d2dc41c calloc + 40
5  libobjc.A.dylib                0x18cd18160 class_createInstance + 76
6  CoreFoundation                 0x18e2b2928 __CFAllocateObject + 28
7  CoreFoundation                 0x18e29c064 +[__NSSingleObjectArrayI __new::] + 28
8  CoreFoundation                 0x18e18cd18 -[NSArray initWithArray:range:copyItems:] + 400
9  MyApp                          0x10010003c -[ConstituentDownload currentProgress] (ConstituentDownload.m:117)
10 MyApp                          0x1001000b4 -[ConstituentDownload currentProgress] (ConstituentDownload.m:118)
11 MyApp                          0x1001000b4 -[ConstituentDownload currentProgress] (ConstituentDownload.m:118)
12 MyApp                          0x1001000b4 -[ConstituentDownload currentProgress] (ConstituentDownload.m:118)
13 MyApp                          0x1001000b4 -[ConstituentDownload currentProgress] (ConstituentDownload.m:118)
14 MyApp                          0x1001000b4 -[ConstituentDownload currentProgress] (ConstituentDownload.m:118)
15 MyApp                          0x1001000b4 -[ConstituentDownload currentProgress] (ConstituentDownload.m:118)
16 MyApp                          0x1001000b4 -[ConstituentDownload currentProgress] (ConstituentDownload.m:118)
17 MyApp                          0x1001000b4 -[ConstituentDownload currentProgress] (ConstituentDownload.m:118)
18 MyApp                          0x1001000b4 -[ConstituentDownload currentProgress] (ConstituentDownload.m:118)
19 MyApp                          0x1001000b4 -[ConstituentDownload currentProgress] (ConstituentDownload.m:118)
20 MyApp                          0x100100234 -[ConstituentDownload sendProgressNotification] (ConstituentDownload.m:141)
... button press stuff...

# OS Version: 10.3.1 (14E304)
# Device: iPad 4
# RAM Free: 4.7%
# Disk Free: 12.2%

#0. Crashed: com.apple.main-thread
0  libobjc.A.dylib                0x1bc38692 objc_retain + 1
1  CoreFoundation                 0x1c8acf39 +[__NSArrayI __new:::] + 74
2  CoreFoundation                 0x1c8ae9f1 -[__NSArrayM copyWithZone:] + 174
3  MyApp                          0x189407 -[ConstituentDownload currentProgress] (ConstituentDownload.m:117)
4  MyApp                          0x18999f -[ConstituentDownload userInfoForProgressNotification] (ConstituentDownload.m:180)
5  MyApp                          0x189611 -[ConstituentDownload sendProgressNotification] (ConstituentDownload.m:144)
...button press stuff...

以下是导致崩溃的方法:

- (float)currentProgress {

    float sections = [self.childDownloads count] + 1;
    float referencedProgress = 0.;
    // This line causes the crash - where we copy the property array
    for(ConstituentDownload *d in [self.childDownloads copy]) {
        referencedProgress += d.currentProgress;
    }
    float progress = (super.currentProgress + referencedProgress) / sections;
    return progress;
}

self.childDownloads是一个NSMutableArray,包含此方法所在的相同类型的对象:ConstituentDownload。它可以从其他线程访问,并且可以添加元素,这就是为什么我在迭代之前先复制它。该数组往往包含0-20个对象。

这次崩溃是否可能是由于在另一个线程中改变数组引起的,即使我在这里复制它?

是否可能是由某种内存损坏引起的?如果是这样,你能指出我正确的方向来解决这个问题吗?

是否可能是因为设备上的RAM耗尽?如果是这样,为什么不将此报告为内存不足错误而不是生成崩溃报告?

是的,从某种意义上说,这种方法是递归的。父ConstituentDownload在其每个子下载上调用currentProgress,后者又会在每个子下载中调用它。崩溃堆栈有时只有一个递归调用,有时大约10个左右,如这两个崩溃堆栈中所示。

此故障仅在运行iOS 9和iOS 10的设备上出现,但这可能是因为几乎所有用户都在这两个操作系统版本上。

KERN_PROTECTION_FAILURE究竟是什么意思?

1 个答案:

答案 0 :(得分:1)

我找不到具体说明的具体参考,但我认为您应该假设以这种方式复制可变数组是不安全的。

一般来说,在一个线程中迭代NSMutableArray是不安全的,而从另一个线程中突变它是不安全的。无论copy是如何在幕后实现的,它都必须以某种方式迭代数组才能完成它的工作。

根据您的描述,您可能会在多个不同的主题中添加/删除项目。您是否使用某种锁定或序列化来使线程安全?如果没有,那么这也可能导致崩溃发生。

如果你是,你应该使用相同的锁定来保护这个copy操作。

相关问题