欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

ios block循环引用问题

程序员文章站 2022-08-05 22:44:45
ios开发中,开了arc模式,自动管理内存,如果程序中用到了block就要注意循环引用带来的内存泄露问题了 这几天遇到一个问题,正常页面dismiss的时候是要调用dealloc...

ios开发中,开了arc模式,自动管理内存,如果程序中用到了block就要注意循环引用带来的内存泄露问题了

这几天遇到一个问题,正常页面dismiss的时候是要调用dealloc方法的,但是我的程序就是不调用,研究了好久终于找到了问题出在哪里了

起初的代码如下:


- (void)getmyrelatedshops

{

    [self.loadtimer invalidate];

    self.loadtimer = [nstimer scheduledtimerwithtimeinterval:0.1

                                                 target:discoverview

                                               selector:@selector(loadwaiting)

                                               userinfo:nil

                                                repeats:yes];

    

    sendedrequest = [[findshopservice sharedinstance] getmyrelatedshopswithpageno:pageno

                                                                     successblock:^(tmrequest *request){

                                                                         [self.loadtimer invalidate];

                                                                         [self shoplistrequestfinished:request];

                                                                     }failedblock:^(tmrequest *failedrequest){

                                                                         [self.loadtimer invalidate];

                                                                         [self shoplistrequestfailed:failedrequest];

                                                                     }];

}

代码表面上看起来没有什么问题,但是细细研究就会发现两个问题

1、block中引用到self,self 被block retain,sendedrequest又retain了该block的一根拷贝

2.sendedrequest是在self类中定义赋值,因此是被self retain

因此就形成了循环引用,不会调用dealloc

还有一个问题,只要重复性 timer 还没有被 invalidated,target 对象就会被一直持有而不会被释放。因此当你使用 self 当作 target 时,你就不能期望在 dealloc 中 invalidate timer,因为在 timer 没有被invalidate 之前,dealloc 绝不会被调用。因此,需要找个合适的时机和地方来 invalidate timer,但绝不是在 dealloc 中。

修改如下


- (void)getmyrelatedshops

{

    [self.loadtimer invalidate];

    self.loadtimer = [nstimer scheduledtimerwithtimeinterval:0.1

                                                 target:discoverview

                                               selector:@selector(loadwaiting)

                                               userinfo:nil

                                                repeats:yes];

   

    __unsafe_unretained findshopvc *wfindshopvc = self;

   

    sendedrequest = [[findshopservice sharedinstance] getmyrelatedshopswithpageno:pageno

                                                                     successblock:^(tmrequest *request){

                                                                         [wfindshopvc.loadtimer invalidate];

                                                                         [wfindshopvc shoplistrequestfinished:request];

                                                                     }failedblock:^(tmrequest *failedrequest){

                                                                         [wfindshopvc.loadtimer invalidate];

                                                                         [wfindshopvc shoplistrequestfailed:failedrequest];

                                                                     }];

}

这样就避免了循环引用,页面注销时就会调用dealloc方法了