0x00 内存泄露
项目中的某个页面
给子视图添加了一个 fade
动画
使用的是 CABasicAnimation
这个动画类
在子视图隐藏后,控制器再 dismiss
于是就在动画类 CABasicAnimation
的代理方法中进行 dismiss
操作
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
[self dismissViewControllerAnimated:NO completion:nil];
}
这个逻辑没毛病吧
然而!
在一次无意之中
发现了控制器的 dealloc
方法木有被调用
GG!
内存泄露了
芭比 Q 了!
0x01 漫长的检查过程
凭借多年的经验
一般是 Block
相关的代码造成的内存泄露
其次就是 NSTimer
了
检查完控制器
并没有使用 NSTimer
相关的 Block
代码也都使用了 weakSelf
继续检查父类
也没有出错的代码
真的是要 GG
了么?
0x02 罪魁祸首
检查了好几遍
都没发现问题
直到第 2
天
在全局搜索 self
这个关键词时
发现动画代码设置相关代理是 self
fadeInAnimation.delegate = self;
代理一般不都用 weak
的嘛
一般都这样写
写多了
谁会在意呢?
结果还是耐着性子点进去
查看了一下系统的源码
what the f**k!
/* The delegate of the animation. This object is retained for the
* lifetime of the animation object. Defaults to nil. See below for the
* supported delegate methods. */
@property(nullable, strong) id <CAAnimationDelegate> delegate;
特么的,居然使用 strong!!!
简直是不讲武德啊!
0x03 解决办法
既然知道了原因
就能有相应的对策了
1.在代理方法中先移除动画
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
[self.button.layer removeAnimationForKey:@"fadeKey"];
[self dismissViewControllerAnimated:NO completion:nil];
}
2.不设置动画的代理 delegate
使用 GCD
的延迟方法来处理
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:NO completion:nil];
});
搞定!
0x04 我的作品
欢迎体验我的作品之一:小五笔小程序
五笔学习好帮手!
微信
搜索即可~