我发现了一个僵尸......现在怎么样?

时间:2016-02-10 18:08:43

标签: ios swift nszombie

我的应用中有以下导航流程:

视图控制器A嵌入在导航控制器中 A模仿B。 在用户解除B之前,导航控制器的视图控制器设置为:[A,C] B被解雇后,C在屏幕上。

这里是如何从B设置视图控制器:

let appDelegate  = UIApplication.sharedApplication().delegate as! AppDelegate
let rootViewController = appDelegate.window!.rootViewController as! RootNavigationController

let Ccontroller = UIViewController()

var newControllers = rootViewController.viewControllers

newControllers.append(Ccontroller)
rootViewController.viewControllers = newControllers

dispatch_async(dispatch_get_main_queue(), { () -> Void in
  self.dismissViewControllerAnimated(true, completion: nil)
})

在按下控制器C上的后退按钮后检测到僵尸。崩溃不一致。有时我会经历一次导航流程,有时我会在接收到崩溃之前执行15次步骤。

我查看了与在解除分配的对象和僵尸上调用函数相关的所有问题,但大多数都与Objective-C中的retain / release有关。我正在寻找Swift解决方案。

最常见的崩溃日志:

*** -[CALayer retain]: message sent to deallocated instance 0x14b7d8880

我们的测试人员发现了一些其他崩溃日志,我认为这些日志与同一个僵尸问题有关。

    Thread : Crashed: com.apple.main-thread
0  libobjc.A.dylib                0x180a780b0 objc_retain + 16
1  UIKit                          0x186617c88 -[UIImageView isAnimating] + 132
2  UIKit                          0x186605630 -[UIImageView stopAnimating] + 112
3  UIKit                          0x186605334 -[UIImageView dealloc] + 64
4  libobjc.A.dylib                0x180a5eb54 object_cxxDestructFromClass(objc_object*, objc_class*) + 148
5  libobjc.A.dylib                0x180a6a040 objc_destructInstance + 92
6  libobjc.A.dylib                0x180a6a0a0 object_dispose + 28
7  UIKit                          0x1869bae80 -[UIResponder dealloc] + 140
8  UIKit                          0x186604ce8 -[UIView dealloc] + 1436
9  UIKit                          0x18677fee0 -[UIControl dealloc] + 72
10 UIKit                          0x18677ffe8 -[UIButton dealloc] + 100
11 UIKit                          0x186a59474 -[UITouch .cxx_destruct] + 136
12 libobjc.A.dylib                0x180a5eb54 object_cxxDestructFromClass(objc_object*, objc_class*) + 148
13 libobjc.A.dylib                0x180a6a040 objc_destructInstance + 92
14 libobjc.A.dylib                0x180a6a0a0 object_dispose + 28
15 UIKit                          0x186643388 -[UITouch dealloc] + 72
16 CoreFoundation                 0x18142e384 __CFBasicHashDrain + 276
17 CoreFoundation                 0x1812e2bdc CFDictionaryRemoveAllValues + 352
18 UIKit                          0x186a51310 -[UITouchesEvent _setHIDEvent:] + 136
19 UIKit                          0x186606dd8 _UIApplicationHandleEventQueue + 3984
20 CoreFoundation                 0x1813b0538 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
21 CoreFoundation                 0x1813affcc __CFRunLoopDoSources0 + 540
22 CoreFoundation                 0x1813adccc __CFRunLoopRun + 724
23 CoreFoundation                 0x1812d9670 CFRunLoopRunSpecific + 384
24 GraphicsServices               0x182bb0088 GSEventRunModal + 180
25 UIKit                          0x186671ec4 UIApplicationMain + 204
26 Slide                          0x100062a0c main (AppDelegate.swift:14)
27 libdispatch.dylib              0x180e768b8 (Missing)

_

Thread : Crashed: com.apple.main-thread
0  QuartzCore                     0x24bfe690 CA::Layer::model_layer(CA::Transaction*) + 51
1  QuartzCore                     0x24bfe64d -[CALayer modelLayer] + 32
2  QuartzCore                     0x24bfe64d -[CALayer modelLayer] + 32
3  QuartzCore                     0x24bfe4e9 -[CALayer animationForKey:] + 56
4  UIKit                          0x26b0fbb5 -[UIImageView isAnimating] + 136
5  UIKit                          0x26afdee3 -[UIImageView stopAnimating] + 102
6  UIKit                          0x26afdc33 -[UIImageView dealloc] + 66
7  libobjc.A.dylib                0x22169f55 object_cxxDestructFromClass(objc_object*, objc_class*) + 116
8  libobjc.A.dylib                0x22173e47 objc_destructInstance + 34
9  libobjc.A.dylib                0x22173e6b object_dispose + 14
10 UIKit                          0x26e99ccd -[UIResponder dealloc] + 128
11 UIKit                          0x26afd635 -[UIView dealloc] + 1428
12 UIKit                          0x26c7368d -[UIControl dealloc] + 64
13 UIKit                          0x26c73789 -[UIButton dealloc] + 96
14 UIKit                          0x26f2c2ab -[UITouch .cxx_destruct] + 126
15 libobjc.A.dylib                0x22169f55 object_cxxDestructFromClass(objc_object*, objc_class*) + 116
16 libobjc.A.dylib                0x22173e47 objc_destructInstance + 34
17 libobjc.A.dylib                0x22173e6b object_dispose + 14
18 UIKit                          0x26b3ae99 -[UITouch dealloc] + 64
19 libobjc.A.dylib                0x22184f67 objc_object::sidetable_release(bool) + 150
20 CoreFoundation                 0x229f0058 __CFBasicHashDrain + 336
21 CoreFoundation                 0x228e1aa5 CFDictionaryRemoveAllValues + 276
22 UIKit                          0x26f264a5 -[UITouchesEvent _setHIDEvent:] + 120
23 UIKit                          0x26aff2ef _UIApplicationHandleEventQueue + 3366
24 CoreFoundation                 0x2298768f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 14
25 CoreFoundation                 0x2298727d __CFRunLoopDoSources0 + 452
26 CoreFoundation                 0x229855eb __CFRunLoopRun + 794
27 CoreFoundation                 0x228d8bf9 CFRunLoopRunSpecific + 520
28 CoreFoundation                 0x228d89e5 CFRunLoopRunInMode + 108
29 GraphicsServices               0x23b24ac9 GSEventRunModal + 160
30 UIKit                          0x26b68ba1 UIApplicationMain + 144
31 Slide                          0xccfbc main (AppDelegate.swift:14)
32 libdispatch.dylib              0x22587873 (Missing)

enter image description here

您将如何确定导致此问题的代码?

1 个答案:

答案 0 :(得分:4)

通常,当您的ViewController被取消分配时会发生此崩溃。避免此崩溃的最佳做法是将ViewController作为属性。为它写getter方法。

@property (nonatomic, strong) ViewController *aViewController;

(ViewController*)aViewController{

     if(_aViewController == nil){
           UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];

           _aViewController = (aViewController*)[mainStoryboard 
                    instantiateViewControllerWithIdentifier: @"ViewControllerIdentifier"];
      }
      return aViewController;
}


 - (IBAction)pushViewController{
        [self.navigationController pushViewController:self.aViewController animated:YES];
}

避免在导航方法中创建ViewController的实例。只需制作一个参考文献并一次又一次地使用它。在导航方法中一次又一次地创建新引用时,可能会释放先前的viewcontroller实例。