内存泄漏

本文探讨了iOS应用中定时器未正确释放导致的dealloc方法未被调用的问题。通过具体示例说明如何在viewWillDisappear或back按钮事件中取消定时器,确保内存正常释放。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1。  代理 weak解决 循环引用

2 ,block 实例变量

3, 注册观察着,要在delloc 中注销 观察着

4,timer

刚调试程序时发现一个很诡异的问题,我从ViewController A push进 ViewController B,在从B back时发现程序不会执行B里面的delloc(),很诡异的问题,因为按理说此时点击back是执行pop操作的,是会执行delloc()函数的,但经调试发现确实没有执行。

后来经过google发现,

The dealloc method was not being called if any of the references held by a viewcontroller were still in memory.
也就是说,我当前View B 内存中计数器不为0,经排查发现,我是在ViewDidLoad函数里面开启了一个time类型,

timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];

这一句调用的时候对 target:self 进行了retain,这时候你pop回上一级,self的引用计数还剩1,所以铁定不会dealloc,我的解决办法是在- (void)viewWillDisappear:(BOOL)animated()

将定时器注销掉 , 对象的引用计数 为0 方可走 delloc


NSTimer

@interface ForgetPasswordViewController ()
{
    NSTimer *timer;
    int  count;
}

  count = 60;
            timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateBtnState) userInfo:nil repeats:YES];

-(void)updateBtnState
{
    if (count == 0) {
        if (timer) {
            [timer invalidate];
            timer = nil;
        }
        self.timeLabel.text = @"获取验证码";
        self.getCodeBtn.enabled = YES;
        self.codeStr = nil;
    }else
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            //更新UI操作
            self.timeLabel.text = [NSString stringWithFormat:@"%d%@",count,@"秒"];
//            [self.getCodeBtn setTitle:@"" forState:UIControlStateNormal];
            self.getCodeBtn.enabled = NO;
            count--;
        });
    }
    
}


- (IBAction)backBtn:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
    if (timer) {
        [timer invalidate];
    }
}


-delloc{}  才会走


GCD


@implementation RegisterViewController
{
    dispatch_source_t _timer;
    dispatch_source_t _distimer;
}



__block int timeout = 60;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1.0*NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(_timer, ^{
        
        if (timeout <= 0) {
            dispatch_source_cancel(_timer);
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                self.mGetCodeBtn.hidden = NO;
                self.mTimeLabel.text = @"Get code";
                [self.mGetCodeBtn setTitle:@"Get code" forState:UIControlStateNormal];
                [self.mGetCodeBtn setBackgroundColor:[UIColor colorWithHex:@"#D62961"]];
                [self.mGetCodeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            });
        }else
        {
            NSString *strtime = [NSString stringWithFormat:@"%.2d",timeout];
            dispatch_async(dispatch_get_main_queue(), ^{
                
                self.mGetCodeBtn.hidden = YES;
                self.mTimeLabel.text = [NSString stringWithFormat:@"%@s重新发送",strtime];
            });
            timeout -- ;
        }
    });
    dispatch_resume(_timer);
}

- (IBAction)backAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];

   if (timer)

{

timer = nil


}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值