循环引用是一个大家熟悉有陌生的东西, 今天的崩溃也和循环引用有点关系.
崩溃堆栈
Thread 0 (crashed)
0 Hago!__38-[HGSettingViewController viewDidLoad]_block_invoke [MFSettingViewController.m : 94 + 0x0]
Found by: given as instruction pointer in context
1 Hago!__38-[HGSettingViewController viewDidLoad]_block_invoke [MFSettingViewController.m : 93 + 0x3]
Found by: previous frame's frame pointer
2 libdispatch.dylib + 0x1795
Found by: previous frame's frame pointer
3 libdispatch.dylib + 0x1781
Found by: previous frame's frame pointer
4 libdispatch.dylib + 0x5d03
Found by: previous frame's frame pointer
5 CoreFoundation!__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 0x7
Found by: previous frame's frame pointer
6 CoreFoundation!__CFRunLoopRun + 0x34f
Found by: previous frame's frame pointer
7 CoreFoundation!CFRunLoopRunSpecific + 0x1d5
Found by: previous frame's frame pointer
8 CoreFoundation!CFRunLoopRunInMode + 0x67
Found by: previous frame's frame pointer
9 GraphicsServices!GSEventRunModal + 0x4f
Found by: previous frame's frame pointer
10 UIKit + 0x72a51
Found by: previous frame's frame pointer
11 Hago!main [main.mm : 16 + 0xf]
Found by: previous frame's frame pointer
12 libdyld.dylib + 0x34e9
Found by: previous frame's frame pointer
定位到崩溃所在的93,94行代码
@implementation HGSettingViewController
{
NSUInteger _totalFileSize;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = PKLocalizedString(@"Setting");
[self.view addSubview:self.tableView];
[self.view addSubview:self.signoutButton];
WEAKIFYSELF;
[FileModel getCachedSize:^(NSUInteger size, NSUInteger count) {
STRONGIFYSELF
self->_totalFileSize = size;
NSString *totalFileSize = [[NSString stringWithFormat:@"%.2fM", size/ 1024./1024.] stringByReplacingOccurrencesOfString:@"." withString:PKLocalizedString(@".")];
self.cacheSizeLabel.text = totalFileSize;
[self.tableView reloadData];
}];
self.remindSwitch = [[HGSwitch alloc] initWithFrame:CGRectZero];
self.remindSwitch.on = [HGConfigModel loadPrivateUInt64:isAlertPushKey default:YES];//[MFPushModel isOn] ? @"已开启" : @"已关闭";
self.remindSwitch.onTintColor = [UIColor colorWithValue:0xF6D859];
[self.remindSwitch addTarget:self action:@selector(onRemindSwitchButton:) forControlEvents:UIControlEventTouchUpInside];
}
我们看到94行的代码确实有些辣眼睛, 这里使用了self->的语法, 但指针之不能为空的, 所以当self为nil, 这里就崩溃了, 但为什么self会为nil, 我们还可以再dig一下, 这里92行getCachedSize
是一个异步的方法, 在block回调前, VC可能就已经被pop了, 这时候self就是nil了.
解决的办法:
解决的办法1: 在STRONGIFYSELF
后判断下
if (!self) { return; }
使用如下的宏
#define CHECK_STRONGIFY_SELF \
__strong __typeof(self) self = weak_self; \
if (!self) { return; }
有人说, 直接使用_totalFileSize
不就行了?
Xcode会报warning, 对于有强迫症的人是绝对不能忍受的. 而且, 这会造成VC延时释放, 这几乎成了很多疑难杂症的万恶之源! VC都释放了, block里的操作还有什么意义? 只有极少数情况是需要在VC释放了继续处理block的, 而且通常是设计不合理造成的.
Block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior
有人说, 使用block
里用weak
, 不用strong
weak_self->_totalFileSize = size;
也不行, 会直接编译不过.
Dereferencing a __weak pointer is not allowed due to possible null value caused by race condition, assign it to strong variable first
解决办法2: 把成员变成属性
[FileModel getCachedSize:^(NSUInteger size, NSUInteger count) {
STRONGIFYSELF
self.totalFileSize = size;
NSString *totalFileSize = [[NSString stringWithFormat:@"%.2fM", size / 1024. / 1024.] stringByReplacingOccurrencesOfString:@"." withString:PKLocalizedString(@".")];
self.cacheSizeLabel.text = totalFileSize;
[self.tableView reloadData];
}];