iOS 应用程序崩溃并出现与 _NSFastEnumerationMutationHandler 相关的异常

Posted

技术标签:

【中文标题】iOS 应用程序崩溃并出现与 _NSFastEnumerationMutationHandler 相关的异常【英文标题】:iOS App crash with exception related to _NSFastEnumerationMutationHandler 【发布时间】:2013-10-17 07:36:44 【问题描述】:

我目前正在开发一个 ios 应用程序,该应用程序崩溃并生成下面提供的崩溃报告。我们认为该应用程序在快速枚举时由于突变而崩溃,但 NSISEngine 中发生了错误。

我们也认为这个问题与表视图的约束有关。此表视图显示从 Rest Services 获取的信息,我们正在以编程方式更新 viewWillAppear() 中的 DataSource。

我们以编程方式添加的一些约束如下:

Method: 

1.cellForRowAtIndexPath()

约束:

a. Updated UILabel with attributed String to set line height: 
   [attributedString addAttribute:NSFontAttributeName value:self.font range:NSMakeRange(0, text.length)];
   self.attributedText = attributedString;

b. Updated UIImage:
   myCell.myLogo.layer.masksToBounds =YES;
   myCell.myLogo.layer.opaque = NO;
   [[myCell.myLogo layer] setCornerRadius:4.];

c. Border for Table View cell
   mySecondCell.secondCellContainerView.layer.masksToBounds = YES;
   [mySecondCell.secondCellContainerView.layer setCornerRadius:3.7];
   [mySecondCell.secondCellContainerView addBorderWithColorRed:@214 Green:@214 Blue:@214];
   [myCell.myLogo addBorderWithColorRed:@214 Green:@214 Blue:@214];
    heightForRowAtIndexPath()

约束: 我们是根据动态内容来计算高度的。

我们正在使用 StoryBoard,其中一些约束是使用 Auto Layout 设置的。

我进行了很多搜索,但找不到与我的应用生成的崩溃报告类似的崩溃报告。我是 iOS 编程的新手。有人可以帮我调试这个崩溃报告吗?如果存在导致此问题的约束,有人可以帮我解决这个问题吗?提前致谢。

    Incident Identifier: F155875B-9FD3-425F-85B9-7595617AF52A
    CrashReporter Key:   aaa79870161ff1e371394b881a26e97f0066f464
    Hardware Model:      iPhone5,2
    Process:             myapp [611]
    Path:                /var/mobile/Applications/40D6D77C-1DBB-4F6B-BEE1-73A67C789C49/myapp.app/myapp
    Identifier:          com.myapp
    Version:             1.0 (0.9)
    Code Type:           ARM (Native)
    Parent Process:      launchd [1]

    Date/Time:           2013-10-17 11:16:45.559 +0530
    OS Version:          iOS 7.0.2 (11A501)
    Report Version:      104

    Exception Type:  EXC_CRASH (SIGTRAP)
    Exception Codes: 0x0000000000000000, 0x0000000000000000
    Triggered by Thread:  0

    Last Exception Backtrace:
    0   CoreFoundation                 0x2fc9fe86 __exceptionPreprocess + 126
    1   libobjc.A.dylib                0x39f9a6c2 objc_exception_throw + 34
    2   CoreFoundation                 0x2fc9f974 __NSFastEnumerationMutationHandler + 124
    3   Foundation                     0x30604f32 -[NSISEngine substituteOutAllOccurencesOfBodyVar:withExpression:] + 438
    4   Foundation                     0x30607ba2 -[NSISEngine pivotToMakeBodyVar:newHeadOfRowWithHead:andDropRow:] + 334
    5   Foundation                     0x3060594c -[NSISEngine minimizeConstantInObjectiveRowWithHead:] + 232
    6   Foundation                     0x3060546e -[NSISEngine optimize] + 170
    7   Foundation                     0x306011a8 -[NSISEngine withBehaviors:performModifications:] + 312
    8   UIKit                          0x32424490 -[UIView(Hierarchy) _postMovedFromSuperview:] + 292
    9   UIKit                          0x3243114e -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1398
    10  UIKit                          0x32447a9c -[UIView(Hierarchy) insertSubview:atIndex:] + 32
    11  UIKit                          0x325fb888 -[UINavigationBar _wrapView:inClippingViewWithLeftBoundary:rightBoundary:leftMaskImage:leftMaskIsChevron:rightMaskImage:] + 240
    12  UIKit                          0x325b505e -[UINavigationBar _startPopAnimationFromItems:fromBarStyle:toItems:toBarStyle:] + 1982
    13  UIKit                          0x324f1b88 -[UINavigationBar popNavigationItem] + 1316
    14  UIKit                          0x324f14a0 -[UINavigationBar _popNavigationItemWithTransition:] + 556
    15  UIKit                          0x326f02ac ___popViewControllerNormal_block_invoke + 152
    16  UIKit                          0x324d43a0 -[UINavigationController _startDeferredTransitionIfNeeded:] + 868
    17  UIKit                          0x324d3fe4 -[UINavigationController __viewWillLayoutSubviews] + 40
    18  UIKit                          0x324d3f78 -[UILayoutContainerView layoutSubviews] + 180
    19  UIKit                          0x3242552e -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 342
    20  QuartzCore                     0x320acf3e -[CALayer layoutSublayers] + 138
    21  QuartzCore                     0x320a8762 CA::Layer::layout_if_needed(CA::Transaction*) + 346
    22  QuartzCore                     0x320a85f4 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 12
    23  QuartzCore                     0x320a8008 CA::Context::commit_transaction(CA::Transaction*) + 224
    24  QuartzCore                     0x320a7e1a CA::Transaction::commit() + 310
    25  QuartzCore                     0x320d5436 CA::Transaction::release_thread(void*) + 158
    26  libsystem_pthread.dylib        0x3a5af9b0 _pthread_tsd_cleanup + 160
    27  libsystem_pthread.dylib        0x3a5af732 _pthread_exit + 82
    28  libsystem_pthread.dylib        0x3a5b04b8 pthread_exit + 24
    29  Foundation                     0x305d7ac6 +[NSThread exit] + 6
    30  Foundation                     0x30683de0 __NSThread__main__ + 1088
    31  libsystem_pthread.dylib        0x3a5b0c58 _pthread_body + 136
    32  libsystem_pthread.dylib        0x3a5b0bca _pthread_start + 98
    33  libsystem_pthread.dylib        0x3a5aeccc thread_start + 4


    Thread 0 Crashed:
    0   libsystem_malloc.dylib         0x3a572596 szone_free_definite_size + 1510
    1   UIFoundation                   0x3754aab4 -[NSAttributeDictionaryEnumerator dealloc] + 68
    2   libobjc.A.dylib                0x39fa5b06 objc_object::sidetable_release(bool) + 170
    3   libobjc.A.dylib                0x39f9701e (anonymous namespace)::AutoreleasePoolPage::pop(void*) + 354
    4   QuartzCore                     0x320a1b54 CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 64
    5   CoreFoundation                 0x2fc6af6e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 18
    6   CoreFoundation                 0x2fc688fa __CFRunLoopDoObservers + 282
    7   CoreFoundation                 0x2fc68c46 __CFRunLoopRun + 734
    8   CoreFoundation                 0x2fbd353c CFRunLoopRunSpecific + 520
    9   CoreFoundation                 0x2fbd331e CFRunLoopRunInMode + 102
    10  GraphicsServices               0x3490a2e6 GSEventRunModal + 134
    11  UIKit                          0x3248a1e0 UIApplicationMain + 1132
    12  myapp                        0x000aac16 main (main.mm:16)
    13  libdyld.dylib                  0x3a493ab4 start + 0

    Thread 1:
    0   libsystem_kernel.dylib         0x3a537838 kevent64 + 24
    1   libdispatch.dylib              0x3a4860d0 _dispatch_mgr_invoke + 228
    2   libdispatch.dylib              0x3a48063e _dispatch_mgr_thread + 34

    Thread 2 name:  com.apple.NSURLConnectionLoader
    Thread 2:
    0   libsystem_kernel.dylib         0x3a537a84 mach_msg_trap + 20
    1   libsystem_kernel.dylib         0x3a53787c mach_msg + 36
    2   CoreFoundation                 0x2fc6a55c __CFRunLoopServiceMachPort + 152
    3   CoreFoundation                 0x2fc68c7c __CFRunLoopRun + 788
    4   CoreFoundation                 0x2fbd353c CFRunLoopRunSpecific + 520
    5   CoreFoundation                 0x2fbd331e CFRunLoopRunInMode + 102
    6   Foundation                     0x3060e64c +[NSURLConnection(Loader) _resourceLoadLoop:] + 316
    7   Foundation                     0x30683dc2 __NSThread__main__ + 1058
    8   libsystem_pthread.dylib        0x3a5b0c5a _pthread_body + 138
    9   libsystem_pthread.dylib        0x3a5b0bca _pthread_start + 98
    10  libsystem_pthread.dylib        0x3a5aeccc thread_start + 4

    Thread 3 name:  com.apple.CFSocket.private
    Thread 3:
    0   libsystem_kernel.dylib         0x3a54a440 select$DARWIN_EXTSN + 20
    1   CoreFoundation                 0x2fc6e45e __CFSocketManager + 482
    2   libsystem_pthread.dylib        0x3a5b0c5a _pthread_body + 138
    3   libsystem_pthread.dylib        0x3a5b0bca _pthread_start + 98
    4   libsystem_pthread.dylib        0x3a5aeccc thread_start + 4
    [12:37:06 PM] Devika Deshmukh: Thread 4:
    0   libsystem_kernel.dylib         0x3a537a84 mach_msg_trap + 20
    1   libsystem_kernel.dylib         0x3a53787c mach_msg + 36
    2   CoreFoundation                 0x2fc6a55c __CFRunLoopServiceMachPort + 152
    3   CoreFoundation                 0x2fc68c7c __CFRunLoopRun + 788
    4   CoreFoundation                 0x2fbd353c CFRunLoopRunSpecific + 520
    5   CoreFoundation                 0x2fbd331e CFRunLoopRunInMode + 102
    6   Foundation                     0x305c1822 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 250
    7   Foundation                     0x30612664 -[NSRunLoop(NSRunLoop) run] + 76
    8   myapp                        0x00197bdc +[AFURLConnectionOperation networkRequestThreadEntryPoint:] (AFURLConnectionOperation.m:160)
    9   Foundation                     0x30683dc2 __NSThread__main__ + 1058
    10  libsystem_pthread.dylib        0x3a5b0c5a _pthread_body + 138
    11  libsystem_pthread.dylib        0x3a5b0bca _pthread_start + 98
    12  libsystem_pthread.dylib        0x3a5aeccc thread_start + 4

    Thread 5:
    0   libsystem_c.dylib              0x3a4fb09e __abort + 102
    1   libsystem_c.dylib              0x3a4fb034 abort + 84
    2   myapp                        0x001bd8ea ___lldb_unnamed_function5196$$myapp + 22
    3   CoreFoundation                 0x2fca018a __handleUncaughtException + 578
    4   libobjc.A.dylib                0x39f9a924 _objc_terminate() + 172
    5   libc++abi.dylib                0x399601b0 std::__terminate(void (*)()) + 76
    6   libc++abi.dylib                0x3995fa04 __cxa_throw + 112
    7   libobjc.A.dylib                0x39f9a796 objc_exception_throw + 246
    8   CoreFoundation                 0x2fc9f974 __NSFastEnumerationMutationHandler + 124
    9   Foundation                     0x30604f32 -[NSISEngine substituteOutAllOccurencesOfBodyVar:withExpression:] + 438
    10  Foundation                     0x30607ba2 -[NSISEngine pivotToMakeBodyVar:newHeadOfRowWithHead:andDropRow:] + 334
    11  Foundation                     0x3060594c -[NSISEngine minimizeConstantInObjectiveRowWithHead:] + 232
    12  Foundation                     0x3060546e -[NSISEngine optimize] + 170
    13  Foundation                     0x306011a8 -[NSISEngine withBehaviors:performModifications:] + 312
    14  UIKit                          0x32424490 -[UIView(Hierarchy) _postMovedFromSuperview:] + 292
    15  UIKit                          0x3243114e -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1398
    16  UIKit                          0x32447a9c -[UIView(Hierarchy) insertSubview:atIndex:] + 32
    17  UIKit                          0x325fb888 -[UINavigationBar _wrapView:inClippingViewWithLeftBoundary:rightBoundary:leftMaskImage:leftMaskIsChevron:rightMaskImage:] + 240
    18  UIKit                          0x325b505e -[UINavigationBar _startPopAnimationFromItems:fromBarStyle:toItems:toBarStyle:] + 1982
    19  UIKit                          0x324f1b88 -[UINavigationBar popNavigationItem] + 1316
    20  UIKit                          0x324f14a0 -[UINavigationBar _popNavigationItemWithTransition:] + 556
    21  UIKit                          0x326f02ac ___popViewControllerNormal_block_invoke + 152
    22  UIKit                          0x324d43a2 -[UINavigationController _startDeferredTransitionIfNeeded:] + 870
    23  UIKit                          0x324d3fe4 -[UINavigationController __viewWillLayoutSubviews] + 40
    24  UIKit                          0x324d3f78 -[UILayoutContainerView layoutSubviews] + 180
    25  UIKit                          0x3242552e -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 342
    26  QuartzCore                     0x320acf3e -[CALayer layoutSublayers] + 138
    27  QuartzCore                     0x320a8762 CA::Layer::layout_if_needed(CA::Transaction*) + 346
    28  QuartzCore                     0x320a85f4 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 12
    29  QuartzCore                     0x320a8008 CA::Context::commit_transaction(CA::Transaction*) + 224
    30  QuartzCore                     0x320a7e1a CA::Transaction::commit() + 310
    31  QuartzCore                     0x320d5436 CA::Transaction::release_thread(void*) + 158
    32  libsystem_pthread.dylib        0x3a5af9b2 _pthread_tsd_cleanup + 162
    33  libsystem_pthread.dylib        0x3a5af732 _pthread_exit + 82
    34  libsystem_pthread.dylib        0x3a5b04b8 pthread_exit + 24
    35  Foundation                     0x305d7ac6 +[NSThread exit] + 6
    36  Foundation                     0x30683de0 __NSThread__main__ + 1088
    37  libsystem_pthread.dylib        0x3a5b0c5a _pthread_body + 138
    38  libsystem_pthread.dylib        0x3a5b0bca _pthread_start + 98
    39  libsystem_pthread.dylib        0x3a5aeccc thread_start + 4

    Thread 6:
    0   libsystem_kernel.dylib         0x3a54ac7c __workq_kernreturn + 8
    1   libsystem_pthread.dylib        0x3a5aee06 _pthread_wqthread + 306
    2   libsystem_pthread.dylib        0x3a5aecc0 start_wqthread + 4

    Thread 0 crashed with ARM Thread State (32-bit):
        r0: 0x00280a00    r1: 0xfffff000      r2: 0x17f9a000      r3: 0x0000000c
        r4: 0x17f99650    r5: 0x3c32fe08      r6: 0x3c32fe00      r7: 0x27d5cb50
        r8: 0x00000001    r9: 0x00000fff     r10: 0x17f99650     r11: 0x0000008a
        ip: 0x17f99db0    sp: 0x27d5cb4c      lr: 0x39f96e39      pc: 0x3a572596
      cpsr: 0x20000030

更新:

以下是我们填充数据源的代码:

(void) viewWillAppear:(BOOL)animated 
   [super viewWillAppear:animated];
   viewWillAppearFlag = true;
   if(dataFetched) 
       [self setupDatasourceDetails:self.myData];
   



(void) setupDatasourceDetails:(NSArray *)data 
   self.datasource = [MyDatasource new];
   self.datasource.data=data;
   [self.myTable setDataSource:self.datasource];
   [self.myTable setDelegate:self.datasource];
   [self.myTable reloadData];



//Callback from REST services
(void) onDataFetched:(NSArray *)data
   dataFetched=true;
   self.myData=data;
   if(viewWillAppearFlag)
[self setupDatasourceDetails:self.myData]; //the event which will occur later(either viewWillAppear or dataFetched) calls setupDatasourceDetails
 

更新:

从一个视图控制器转换回另一个视图控制器时发生错误(在导航控制器中)。

我们正在使用 [self.navigationController popViewControllerAnimated:YES];转换回前一个视图控制器。转换发生在主线程上。

如果需要更多信息,请告诉我们。

更新

看来我们的问题已经解决了。我们将功能从 viewWillAppear 移到 viewDidAppear。此修复似乎已解决该问题。有谁知道这个修复工作的原因?请告诉我们。谢谢。

【问题讨论】:

你能告诉我们你更新数据源的代码吗?我想后台网络获取可能存在线程问题 我们已经用相关代码更新了问题。谢谢。 【参考方案1】:

两个建议:

    检查 self.datasource 是否定义为 strong 属性(不是 weak)(以及MyDatasource 上的数据属性)

    确保 onDataFetched: 在主队列上被调用。如果不确定,请使用此 而是调度机制:

       dispatch_async(dispatch_get_main_queue(), ^
            // all update code here
       );
    

【讨论】:

我们在代码中遵循了您提到的调度机制。碰撞的频率略有减少,但并未完全消除。我们仍然面临崩溃。 我们通过修复更新了问题。感谢您的建议。 如果你把它移到-viewDidAppear,它们还会出现吗?【参考方案2】:

我不认为错误直接出现在您发布的代码中,但是:

 NSFastEnumerationMutationHandler

当您在枚举时编辑数组或字典时引发。 例如这是错误的:

for ( id obj in anArray ) 
    if ( [obj shouldRemove == YES ) 
        [anArray removeObject:obj];
    

如果您需要这样做,您应该使用数组的副本或使用 filtersArrayUsingPredicate。

【讨论】:

您提供的答案是正确的。但是枚举似乎发生在 NSISEngine 内部,而不是直接在我们的代码中。这就是我们无法找出导致此问题的确切代码的原因。感谢您的建议。 也许你正在编辑一些视图,而他们正在动画回来或什么?我看到一些块调用 - 它在主线程上吗? 我们正在调用 [self.navigationController popViewControllerAnimated:YES];在 NavigationController 中导航回来。您是在谈论崩溃报告中的块吗?我们不确定它是否发生在主线程中。那应该在主线程上吗?我将检查它是否在主线程上被调用并相应地更新我的问题。谢谢。 转换发生在主线程上。我们用详细信息更新了问题。谢谢。【参考方案3】:

看不到您的所有代码,但我认为您需要在设置约束之前调用 layoutIfNeeded,因为在 ViewWillAppear 之前未计算视图。它在 ViewDidAppear 中成功的事实是因为已经计算了元素。或者,您可以在 DidLayoutSubviews 中尝试您的代码。

【讨论】:

这不是一个答案,但它会是一个很好的评论。

以上是关于iOS 应用程序崩溃并出现与 _NSFastEnumerationMutationHandler 相关的异常的主要内容,如果未能解决你的问题,请参考以下文章

iOS6 上的 GKLocalPlayer 身份验证崩溃并出现模态转换错误

使用 UIMotionEffect 类在 IOS 6 中启动应用程序时崩溃

iOS 崩溃 > TCCAccessRequest_block_invoke_2.8

如何调查 iOS 崩溃并出现错误 NSOSStatusErrorDomain Code=-50 "query missing class name"

iOS 应用程序奇怪的崩溃 [UINavigationController _startCustomTransition:]

在iOS 11上出现libsystem_kernel.dylib`__abort_with_payload崩溃问题的解决