前言
iOS崩溃是让iOS开发人员比较头痛的事情,app崩溃了,说明代码写的有问题,这时如何快速定位到崩溃的地方很重要。调试阶段是比较容易找到出问题的地方的,但是已经上线的app并分析崩溃报告就比较麻烦了。
本文将给大家总结介绍关于iOS中多线程的一些经典崩溃,下面话不多说了,来一起看看详细的介绍吧。
0x0 Block 回调的崩溃
在MRC环境下,使用Block 来设置下载成功的图片。当self释放后,weakSelf变成野指针,接着就悲剧了
__block ViewController *weakSelf = self;
[self.imageView imageWithUrl:@"" completedBlock:^(UIImage *image, NSError *error) {
NSLog(@"%@",weakSelf.imageView.description);
}];
0x1 多线程下Setter 的崩溃
Getter & Setter 写多了,在单线程的情况下,是没有问题的。但是在多线程的情况下,可能会崩溃。因为[_imageView release]; 这段代码可能会被执行两次,oops!
UIKit 不是线程,所以在不是主线程的地方调用UIKit 的东西,有可能在开发阶段完全没问题,直接免测。但是一到线上,崩溃系统可能都是你的崩溃日志。Holy shit!
解决办法:通过hook 住setNeedsLayout,setNeedsDisplay,setNeedsDisplayInRect来检查当前调用的线程是否是主线程。
- (void)setImageView:(UIImageView *)imageView
{
if (![_imageView isEqual:imageView])
{
[_imageView release];
_imageView = [imageView retain];
}
}
0x2 更多Setter 类型的崩溃
property 的属性,写的最多的就是nonatomic,一般情况下也是没有问题的!
@interface ViewController ()
@property (strong,nonatomic) NSMutableArray *array;
@end
跑一下下面这段代码,你会看到:
malloc: error for object 0x7913d6d0: pointer being freed was not allocated
for (int i = 0; i < 100; i++) {
dispatch_async(dispatch_get_gl