block循环引用问题
2015-10-12 18:08
316 查看
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方法了
关于block的详细解释可参考 http://www.cnblogs.com/kesalin/archive/2013/04/30/ios_block.html
这几天遇到一个问题,正常页面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方法了
关于block的详细解释可参考 http://www.cnblogs.com/kesalin/archive/2013/04/30/ios_block.html
相关文章推荐
- xcode使用命令行打包ipa
- 根据浏览猜你喜欢 换一批功能
- ios测试基础一:mac下HTTP与HTTPS抓包
- 改进的混合高斯背景建模
- 服务器加速
- thinkphp--标签库
- POJ 1149 PIGS
- 标准模板库(四):STL的算法中的函数介绍
- 微信支付V3版开发中遇到的一个问题及原因:缺少prepay_id
- cocopods的简单操作
- springmvc 全局登陆 拦截器配置
- jquery datepicker 设置只显示周一和周末
- 前端事件系统(三)
- 获取设备信息以及APP版本
- 博弈入门
- 通过JavaScript函数向网页中输出数据
- codepath教程:浮动操作按钮详解
- 万能的Java WebService调用程序支持C# JAVA WS调用
- 010redis中数据的排序(sort)
- office文档转化为pdf