美文网首页
controller 不能释放,不走dealloc方法的4种可能

controller 不能释放,不走dealloc方法的4种可能

作者: 悲伤C小调 | 来源:发表于2018-03-27 18:30 被阅读22次

    第一种: controller中使用了计时器 NSTimer 使用后没有销毁 导致循环引用

    self.playerTimer = [NSTimerscheduledTimerWithTimeInterval:1target:selfselector:@selector(playProgressAction)userInfo:nilrepeats:YES];

    使用后记得销毁

    [_playerTimerinvalidate];

    _playerTimer =**nil**;
    

    第二种:协议delegate 应该使用weak修饰,否则会引起循环引用 不能释放内存

    @property (nonatomic,weak)id<huifuDelegate>huifudelegate;

    第三种:使用到block的地方,,block回调中不能直接使用self 否则可能引起循环引用。

    __weak****typeof(self) weakSelf =self;

    _audioStream.onCompletion=^(){
    
        [weakSelf nextButtonAction];
    
    };
    

    第四种:

    对于前三种 大家可能都知道,,我发现一个会导致controller不能释放的情况,,很诡异,,pop后不走dealloc 再push进来会走一次dealloc..这种情况是我接手的工程中出现的问题,,不仅不能释放 ,,如果这个controller中有音频 视频的录制,,应用退入后台会出现麦克风后台使用的提示。 image

    。如果有使用AirPlay 播放音频 ,,也会对AirPlay的时间显示产生干扰。。当然这都是没释放内存引起的。。。。。。。

    具体的情况如下,

    有ViewController和ceshiViewController ViewController要push到ceshiViewController

    import "ViewController.h"

    import "ceshiViewController.h"

    @interfaceViewController (){

    ceshiViewController *ceshiVC;// 使用实例变量声明的时候,,我是不怎么这样写
    

    }

    • (void)action{

      ceshiVC = [[ceshiViewControlleralloc]init];///这样写就出问题了

      [self.navigationControllerpushViewController:ceshiVCanimated:YES];

    }

    /////////////==============///////////////////

    import "ceshiViewController.h"

    @interfaceceshiViewController ()

    @end

    @implementation ceshiViewController

    • (void)dealloc{

      NSLog(@"---释放");

    }

    controller 返回后不会释放。。。。。。。。

    5.。。。。项目中遇到的不走dealloc情况

    @interface TopicDetailViewController (){

    NSInteger videoType;
    

    }

    • (void)addFooterData{

      __weak TopicDetailViewController *weakself = self;

      self.TopicDetailTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{

        [weakself loadDataWithtype:videoType];  
      
        /// 在这里使用videoType后 不走dealloc 换成属性创建,,使用weakself.videoType 可解决
      

      }];

    }

    相关文章

      网友评论

          本文标题:controller 不能释放,不走dealloc方法的4种可能

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