IOS-57-导致内存未释放的常见原因(现象:dealloc不执行等)

来源:互联网 发布:苹果部落冲突数据覆盖 编辑:程序博客网 时间:2024/04/27 22:46

几种原因:
1.代理声明应为weak,默认strong强引用会导致计数器加1,无法释放内存;应这样声明:

@property (nonatomic, weak) SampleClass *sampleClass;

2.NSTimer定时器未释放,会导致计数器加1,无法释放内存。
应这样先关闭定时器:

/** *  开起定时器加载 */- (void)loadingStartAnimation {    _loadTimer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(customAnimation) userInfo:nil repeats:YES];    [_loadTimer fire];}// 关闭定时器- (void)invalidTimer {    if (_loadTimer) {        [_loadTimer invalidate];        _loadTimer = nil;    }}

3.死循环:在getter方法里使用self.
如下:

#pragma mark getter- (UITableView *)meTableView {    if (_meTableView == nil) {        _meTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kMAIN_SCREEN_WIDTH, kMAIN_SCREEN_HEIGHT  - kTAB_BAR_HEIGHT) style:UITableViewStylePlain];        _meTableView.delegate   = self;        _meTableView.dataSource = self;        self.meTableView.backgroundColor = Background_Color;  //本方法是getter方法,不能使用self!!!    }    return _meTableView;}

4.block里面不能直接用self,需转为weak弱类型

__weak typeof(self) weakSelf = self;//转为weak弱类型    [self.network doRequestUsingCookieWithType:TLDRequestType_Get urlString:kGetRechargeRecordURL paramsDic:paramDic fromDelegate:self successBlock:^(AFHTTPRequestOperation *operation, id resultData) {        [weakSelf requestRechargeRecordFinished:resultData];    } failBlock:^(AFHTTPRequestOperation *operation, NSError *error) {        [weakSelf.HUD showToastWithText:kRequestFailString];    }];
0 0