2017年5月5日
一.强引用对象导致页面push后,没有调用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];
//设置绝对时间值
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"];
};
//页面创建就启动
[_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];
//设置绝对时间值
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"];
};
//页面创建就启动
[_coutTimeV startCountDown];
}
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_coutTimeV];
}
}
}
2016年10月19日
1.原因是循环引用和成员变量block内嵌套使用导致
a.block中如果使用了__strong修饰的额对象变量,如self,那么改对象就会被block所持有,导致循环引用,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];
}
如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。