美文网首页
iOS 懒加载在中的奇葩内存泄漏

iOS 懒加载在中的奇葩内存泄漏

作者: YannChee | 来源:发表于2019-02-22 21:06 被阅读0次

    原来代码

    // 有内存泄漏
    - (QYSegmentedView *)segmentBar {
        if (!_segmentBar) {
            QYSegmentedView *segmentBar = [QYSegmentedView segmentedViewWithFrame:CGRectZero];
            segmentBar.backgroundColor = [UIColor brownColor];
            [self.view addSubview:segmentBar];
            
            segmentBar.didSlectedIndexBlock = ^(NSInteger targetIndex, NSInteger sourceIndex) {
                [self showChildVCViewsAtIndex:targetIndex];
            };
            
            _segmentBar = segmentBar;
        }
        return _segmentBar;
    }
    
    
    // 无内存泄漏
    - (QYSegmentedView *)segmentBar {
        if (!_segmentBar) {
            QYSegmentedView *segmentBar = [QYSegmentedView segmentedViewWithFrame:CGRectZero];
            segmentBar.backgroundColor = [UIColor brownColor];
            [self.view addSubview:segmentBar];
            
            __weak __typeof(self)weakSelf = self;;
            segmentBar.didSlectedIndexBlock = ^(NSInteger targetIndex, NSInteger sourceIndex) {
                [weakSelf showChildVCViewsAtIndex:targetIndex];
            };
            
            _segmentBar = segmentBar;
        }
        return _segmentBar;
    }
    

    相关文章

      网友评论

          本文标题:iOS 懒加载在中的奇葩内存泄漏

          本文链接:https://www.haomeiwen.com/subject/eysyyqtx.html