【问题标题】:iOS Crash on copying an NSArray EXC_BAD_ACCESS KERN_PROTECTION_FAILURE复制 NSArray EXC_BAD_ACCESS KERN_PROTECTION_FAILURE 时 iOS 崩溃
【发布时间】:2017-07-12 20:08:21
【问题描述】:

这里有两个来自 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,而后者又在其每个子下载时调用它。崩溃堆栈中有时只有 1 个递归调用,有时大约有 10 个左右,如这两个崩溃堆栈所示。

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

KERN_PROTECTION_FAILURE 到底是什么意思?

【问题讨论】:

    标签: ios objective-c memory crash crashlytics


    【解决方案1】:

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

    一般来说,在一个线程中迭代 NSMutableArray 是不安全的,同时它正在从另一个线程进行变异。无论copy 在底层实现什么方式,它都必须以某种方式遍历数组才能完成其工作。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-05
      • 1970-01-01
      • 2018-11-30
      • 1970-01-01
      • 2022-08-16
      • 2011-12-25
      • 2011-04-10
      • 2013-09-14
      相关资源
      最近更新 更多