arc環(huán)境下 dealloc沒有調(diào)用解決

2017年5月5日
一.強引用對象導(dǎo)致頁面push后,沒有調(diào)用dealloc對象
1.原因block里面用了強引用對象來操作計時器

- (void)initCountTimeView
{
    if (_pageType == HuTestPracticePageTypeDoPaper) {
        if(_examType == HuExamTypeExam){
            if(_coutTimeV == nil){
                NSDictionary *dic = @{fontColorKey:[UIColor blueColor]};

                //test
//                _reqParamM.timeNum = 600;
                _coutTimeV = [[HuCountTimeView alloc]initWithTime:_reqParamM.timeNum AndDic:dic];

                //設(shè)置絕對時間值
                NSInteger nowTime = [HuConfigration getCurrentSeconds];
                _absolutTime = nowTime + _reqParamM.timeNum;

                //時間到了上傳答案
                __block HuTestPracticeViewController *strongSelf = self;
                _coutTimeV.timeOut = ^(){
                    [strongSelf uploadPaper:YES];
                    [strongSelf.coutTimeV endCountDown];
                };
                _coutTimeV.changeColorTimeOut = ^(){
                    strongSelf.coutTimeV.timeL.textColor = [HuConfigration uiColorFromString:@"#f66767"];
                };

                //頁面創(chuàng)建就啟動
                [_coutTimeV startCountDown];
            }
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_coutTimeV];
        }
    }
}

2.修改成如下就可以(目前不需要修改成員變量,所以其實不用__block)

- (void)initCountTimeView
{
    if (_pageType == HuTestPracticePageTypeDoPaper) {
        if(_examType == HuExamTypeExam){
            if(_coutTimeV == nil){
                NSDictionary *dic = @{fontColorKey:[UIColor blueColor]};

                //test
//                _reqParamM.timeNum = 600;
                _coutTimeV = [[HuCountTimeView alloc]initWithTime:_reqParamM.timeNum AndDic:dic];

                //設(shè)置絕對時間值
                NSInteger nowTime = [HuConfigration getCurrentSeconds];
                _absoluteTime = nowTime + _reqParamM.timeNum;

                //時間到了上傳答案(需要修改局部變量)
                WS(weakSelf);
                _coutTimeV.timeOut = ^(){
                    [weakSelf uploadPaper:YES];
                    [weakSelf.coutTimeV endCountDown];
                };
                _coutTimeV.changeColorTimeOut = ^(){
                    weakSelf.coutTimeV.timeL.textColor = [HuConfigration uiColorFromString:@"#f66767"];
                };

                //頁面創(chuàng)建就啟動
                [_coutTimeV startCountDown];
            }
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_coutTimeV];
        }
    }
}

2016年10月19日
1.原因是循環(huán)引用和成員變量block內(nèi)嵌套使用導(dǎo)致
a.block中如果使用了__strong修飾的額對象變量,如self,那么改對象就會被block所持有,導(dǎo)致循環(huán)引用,dealloc沒有被釋放。
b.成員變量 _number 在第一次block使用后,又在第二層block使用


Paste_Image.png

2.解決修改 (改成弱引用,去掉多余的成員變量在blcok嵌套使用)

//對自己弱引用
#define WS(weakSelf)  __weak typeof(self)weakSelf = self;

@interface MyFavoriteViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UILabel *_myLab;
    int _number;
}
- (void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, HHBWIDTH,HHBHEIGHT-40-64-49-40) style:UITableViewStylePlain];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.rowHeight=80;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone ;
    [self.tableView registerNib:[UINib nibWithNibName:@"ListFavriteMapperTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tableView];
    WS(weakSelf)
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        _number = 0;
        [weakSelf tableViewRefreshSelf];
    }];
    [self.tableView.mj_header beginRefreshing];
}

-(void)tableViewRefreshSelf
{
//    _number = 0;
    NSString *nurseId=[[NSUserDefaults standardUserDefaults]objectForKey:@"nurseId"];
    NSDictionary *parameters=@{@"nurseId":nurseId};
    WS(weakSelf)
    [MpHttpTolls listFavrite:parameters success:^(NSArray *data) {
        _number = 0;
       //other action
    } view:weakSelf.view];
}

- (void)dealloc
{
    [kNotificationCenter removeObserver:self];
}

如果您發(fā)現(xiàn)本文對你有所幫助,如果您認(rèn)為其他人也可能受益,請把它分享出去。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容