项目常见崩溃5(陆续更新)

前情提要, 之前围绕着window, present, dismiss展开了一些列的讨论, 但有一点是我们开发应该注意的, 就是我们使用了局部变量, 创建了UIAlertView, 然后, 这个UIAlertView就不在我们的控制范围内了, 那些比较恶心, 没有头绪的崩溃, 其实很多时候是我们开发者使用UI控件不当造成的, 今天奉上另外一例使用UI控件错误导致的崩溃.

崩溃堆栈S

一个崩溃堆栈

0  libobjc.A.dylib!objc_msgSend + 0x10
1  UIKit!-[UINavigationController _startCustomTransition:] + 0x3c8
2  UIKit!-[UINavigationController _startDeferredTransitionIfNeeded:] + 0x1d0
3  UIKit!-[UINavigationController __viewWillLayoutSubviews] + 0x34
4  UIKit!-[UILayoutContainerView layoutSubviews] + 0xc4
5  UIKit!-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 0x238
6  QuartzCore!-[CALayer layoutSublayers] + 0xa4
7  QuartzCore!CA::Layer::layout_if_needed(CA::Transaction*) + 0x13c
8  QuartzCore!CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 0x1c
9  QuartzCore!CA::Context::commit_transaction(CA::Transaction*) + 0x110

另一个崩溃堆栈

0  libobjc.A.dylib!objc_msgSend + 0x10
1  UIKit!-[UINavigationController _startTransition:fromViewController:toViewController:] + 0x388
2  UIKit!-[UINavigationController _startDeferredTransitionIfNeeded:] + 0x27c
3  UIKit!-[UINavigationController __viewWillLayoutSubviews] + 0x34
4  UIKit!-[UILayoutContainerView layoutSubviews] + 0xc4
5  UIKit!-[UIImagePickerController _setupControllersForCurrentSourceType] + 0x50
6  UIKit!-[UIImagePickerController setSourceType:] + 0x21c
7  libdispatch.dylib!_dispatch_call_block_and_release + 0x14
8  libdispatch.dylib!_dispatch_client_callout + 0xc
9  libdispatch.dylib!_dispatch_main_queue_callback_4CF + 0x644

另另一个崩溃堆栈

0  libobjc.A.dylib!objc_msgSend + 0x16
1  UIKit!-[UIImagePickerController _imagePickerDidCompleteWithInfo:] + 0x4b
2  PhotoLibrary!PLNotifyImagePickerOfImageAvailability + 0x49
3  PhotoLibrary!PLNotifyImagePickerOfMediaAvailability + 0x73
4  PhotosUI!-[PUUIImagePickerControllerHelper _notifyImagePickerOfAssetAvailability:] + 0x89
5  PhotosUI!__71-[PUUIImagePickerControllerHelper handleSelectionOfAsset:inCollection:]_block_invoke69 + 0x23
6  PhotosUI!-[PUPhotosGridDownloadHelper _downloadForRequest:didCompleteWithSuccess:canceled:error:] + 0x103
7  PhotosUI!__84-[PUPhotosGridDownloadHelper handleDownloadOfAsset:inCollection:withSuccessHandler:]_block_invoke + 0x47
8  libdispatch.dylib!_dispatch_call_block_and_release + 0x9
9  libdispatch.dylib!_dispatch_client_callout + 0x15

另另另一个崩溃堆栈

0  libobjc.A.dylib!objc_msgSend + 0x10
1  UIKit!-[UIImagePickerController _imagePickerDidCancel] + 0x44
2  PhotosUI!-[PUUIPhotosAlbumViewController _handleImagePickerCancel:] + 0x78
3  UIKit!-[UIApplication sendAction:to:from:forEvent:] + 0x5c
4  UIKit!-[UIApplication sendAction:to:from:forEvent:] + 0x5c
5  MakeFriends!-[UIButton(touch) mySendAction:to:forEvent:] [UIButton+touch.m : 79 + 0x18]
6  UIKit!-[UIControl _sendActionsForEvents:withEvent:] + 0x260
7  UIKit!-[UIControl touchesEnded:withEvent:] + 0x24c
8  UIKit!-[UIWindow _sendTouchesForEvent:] + 0x2b8
9  UIKit!-[UIWindow sendEvent:] + 0x2a8

WTF, 不过大家应该从第一个堆栈的一脸懵逼到第二个堆栈的有点懵懂, 再到第三和第四个堆栈大概知道了, 貌似是相册UIImagePickerController有点问题, 于是你可能会去stack overflow上去搜怎样使用UIImagePickerController

UIImagePickerController *vc = [[UIImagePickerController alloc] init];
vc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
vc.delegate = self;
vc.allowsEditing = YES;
[self presentViewController:vc animated:YES completion:nil];

上面这种用法应该不陌生, 而且很多人估计也是这样做的, 测试时候可能也很难测出问题, 然而, 这就是菜鸟和老鸟的区别, 老鸟一般不会使用系统默认的UI控件, 当你使用UIImagePickerController发现苹果出了3D touch之后你的项目崩的一塌糊涂的时候, 你想起了搜搜搜, 你会自定义一个UIImagePickerController就叫XXUIImagePickerController吧, 当然, XXUIImagePickerController继承自UIImagePickerController. 于是代码变成下面的样子

XXImagePickerController *vc = [[XXImagePickerController alloc] init];
vc.sourceType = XXImagePickerControllerSourceTypePhotoLibrary;
vc.delegate = self;
vc.allowsEditing = YES;
[self presentViewController:vc animated:YES completion:nil];

好吧, 现在没有3D touch的崩溃了, 我们集中说代理的崩溃, 在iOS8及以下的系统上, delegate的默认属性是unsafe_unretained, 啥意思? 不安全, 赋值的时候引用计数不增加. 有关unsafe_unretained的详细介绍请移步这篇文章 这就造成了, 当XXImagePickerController dealloc的时候delegate如果之前赋值过就变成了野指针, 那后面发生任何诡异的问题就都不奇怪了. 所以, 聪明的小伙伴已经知道简单的解决办法了.

XXImagePickerController *vc = [[XXImagePickerController alloc] init];
self.picker = vc;
vc.sourceType = XXImagePickerControllerSourceTypePhotoLibrary;
vc.delegate = self;
vc.allowsEditing = YES;
[self presentViewController:vc animated:YES completion:nil];

- (void)dealloc {
    self.picker.delegate = nil;
}

不瞒小伙伴, 这个崩溃伴随我们项目1年多, 没人去解决, 我也是一个偶然的机会, 在调试的时候发现了这个崩溃, 当然你要打开Zombie, 这个问题关键是量不大, 而且很难重现, 这也是这个问题长久以来一直无法得到关注的原因吧.

总结:

1 iOS8及以下的系统, delegate是unsafe_unretained
2 如果你的项目要兼容iOS8及以下系统就一定要注意不要使用局部控件, 因为主动权不在你这里. 在程序发生一些异常情况需要退出UI的时候, 可能还需要对当前的UI进行一些清理(delegate置空)操作, 如果不这样, 崩溃不可避免.

你可能感兴趣的:(项目常见崩溃5(陆续更新))