美文网首页
ios造成dealloc方法不走的原因

ios造成dealloc方法不走的原因

作者: ttdiOS | 来源:发表于2023-03-05 17:57 被阅读0次

ios造成dealloc方法不走的原因 或者延迟后才走

终于踏进了自己埋的坑!

由于视频详情页需求变更,我删除了以前的代码,新加的代码并没有考虑太多,播放也没有啥问题,就没管。到了后期发现快进快出,请求成功的时候播放器没有销毁,仍然能发出声音,断点发现dealloc方法未走。各种检查各种查,最后发现就是block的问题,都加上 @weakify(self);和@strongify(self);后还是不能解决,最后定位到:

分离出来后问题就解决了。

总结下,造成dealloc不走的主要有三种情况:

1.NSTimer

2.block

3.delegate

nstimer里:

[[ZGZIMManager shared] setChatOnRoomSuccessBlock:^(NSInteger userNumber) {

if (weakSelf.onRoomUserTimer) {

[weakSelf.onRoomUserTimer invalidate];

weakSelf.onRoomUserTimer = nil;

}

weakSelf.onRoomTimes = 15;

// weakSelf.onRoomTimes = 40;

weakSelf.onRoomUserTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:weakSelf selector:@selector(onRoomUserTimerHeadle) userInfo:nil repeats:YES];

if(weakSelf.onRoomUserTimer){

[[NSRunLoop currentRunLoop] addTimer:weakSelf.onRoomUserTimer forMode:NSRunLoopCommonModes];

}

}];

block里有nstimer ,target:用self就会循环引用,不走dealloc,要用 target:weakSelf 

同时 还要判空, if(weakSelf.onRoomUserTimer){

------延迟30s后才走----------

如图:

计时器的block里用了self或者strongself就会延迟30s后才dealloc,用weaself就好了

相关文章

网友评论

      本文标题:ios造成dealloc方法不走的原因

      本文链接:https://www.haomeiwen.com/subject/qoqpldtx.html