问题

在调试程序时,我从ViewController A push进 ViewController B,在从B back时发现程序不会执行B里面的dealloc(),很诡异的问题,因为按理说此时点击back是执行pop操作的,是会执行dealloc()函数的,但经调试发现确实没有执行。所以viewController也就不会销毁.

原因

The dealloc method was not being called if any of the references held by a viewcontroller were still in memory. 也就是说,我当前View B 内存中计数器不为0,还有一些内容的引用计数不为0, 所以造成了 controller 不能及时释放.

解决

我发现在我的代码里有一个倒计时的计时器的功能, 是通过 NSTimer实现的, 
在调用的时候对 target:self 进行了retain, 这时候你pop回上一级,self的引用计数还剩1,所以肯定不会dealloc. 当其倒计时结束后, 我有一个[timer invalidate]的方法, 这个时候才会调用 dealloc.  
所以我的解决办法是在viewWillDisappear: 方法中执行[timer invalidate];这样,self view计数器为0,当前view就会自动调用delloc()。

总结

其实这个问题, 就跟循环引用的问题是类似的, 都是由于内存考虑不周全管理不当造成的. 但是很多时候都会出现, 尤其是在 NSTimer , block , delegate, 使用的时候, 很容易造成当前页面不能及时销毁, 从而导致内存泄露. timer 一定要及时invalidate, block 要用 copy修饰而且还有防止析构和 block块内的循环引用, delegate 要用assign 修饰. 等等都需要好好注意.

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

第一种: 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 否则可能引起循环引用。


   __weaktypeof(self) weakSelf =self;

    _audioStream.onCompletion=^(){

        [weakSelf nextButtonAction];

    };

第四种:

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

    }];

}

问题:viewController不会调用dealloc()不会销毁的更多相关文章

  1. 什么时候调用dealloc

    什么时候回调用dealloc? 1.这个类被release的时候会被调用: 2.这个对象的retain count为0的时候会被调用: 3.或者说一个对象或者类被置为nil的时候:

  2. NSTimer内存泄漏导致控制器不调用dealloc

    创建定时器会在一定的间隔后执行某些操作,一般大家会这样创建定时器,这样创建的定时,self对定时器有个引用,定时器对self也有个引用,造成了循环引用,最终造成了内存泄漏,如果定时器在做下载的操作就会 ...

  3. 对象池3(方法功能)PoolManager(控制)PoolTimeObject(时间管理)text01(调用)Destorys(销毁)

    1.对象池PoolManager namespace kernal { public class PoolManager : MonoBehaviour { //“缓冲池”集合 public stat ...

  4. iOS 面试总结 一

    iOS 开发工程师之面试总结一 好久没有出去面试了,大概一年的时间都很稳定,最近出去面试感觉心里特别慌,没有了当时的勇气了,其实还是感觉自己的准备不是特别的充分,这是主要原因. 这段时间待得太安逸没了 ...

  5. iOS 添加WKWebView导致控制器无法释放的问题

    在WkWebView与JavaScript交互中,经常会在原生中注入MessageHandler,app中注入MessageHandler的方法 WKWebViewConfiguration *con ...

  6. 深入了解UIViewController控制器与对应的View类的详解

    ViewController是iOS开发中MVC模式中的C(视图控制器),ViewController是view的controller,ViewController的职责主要包括管理内部各个view的 ...

  7. Objective-c内存管理

    cocoa中的内存管理机制 引用计数 每一个对象都拥有一个引用计数 当对象创建的时候,引用计数的值是1 当发生retain消息时,该对象的引用计数+1,该对象的引用计数为2 当向这个对象发送relea ...

  8. OC语法6——内存管理之引用计数器(retain,release)

    OC内存管理: 一.引用计数器: Java有垃圾回收机制(Garbage Collection,GC).也就是说当我们创建对象后,不需要考虑回收内存的事,Java的垃圾回收机制会自动销毁该对象,回收它 ...

  9. iOS开发-内存管理

    内存管理 对于这篇呢,其实现在都是ARC模式,正常状态下基本不用我们去手动释放内存,所以如果不是要面试呀.装逼或者扎实功底的,就先别看了或者了解下即可,因为像面试时,有些面试官想看你的基础时,就有些人 ...

随机推荐

  1. mysql 获取系统时间的下一天 年-月-日 时:分:秒

    DAY) as date

  2. static关键字 详解

    原文地址:http://blog.csdn.net/keyeagle/article/details/6708077 google了近三页的关于C语言中static的内容,发现可用的信息很少,要么长篇 ...

  3. 水题:HDU-1088-Write a simple HTML Browser(模拟题)

    解题心得: 1.仔细读题,细心细心...... 2.题的几个要求:超过八十个字符换一行,<br>换行,<hr>打印一个分割线,最后打印一个新的空行.主要是输出要求比较多. 3. ...

  4. 笔记-python-tutorial-8.errors and exceptions

    笔记-python-tutorial-8.errors and exceptions 1.      errors and exceptions 1.1.    syntax errors >& ...

  5. 为什么要用枚举实现Singleton--Java

    原文地址:http://www.cnblogs.com/AprilCal/p/5426007.html 理由一:无需再考虑可序列化的情况   <effective java>第77条:对于 ...

  6. RDLC Reporting in Visual Studio 2017

    原文:RDLC Reporting in Visual Studio 2017 Visual Studio 2017 中可以使用 RDLC Reporting 插件来设计报表,SAP Crystal ...

  7. MVC6学习教程

    http://www.cnblogs.com/TomXu/p/4495251.html

  8. 【Combination Sum 】cpp

    题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C  ...

  9. leetcode 【 Linked List Cycle II 】 python 实现

    公司和学校事情比较多,隔了好几天没刷题,今天继续刷起来. 题目: Given a linked list, return the node where the cycle begins. If the ...

  10. python 学习分享-实战篇选课系统

    # 角色:学校.学员.课程.讲师 # 要求: # 1. 创建北京.上海 2 所学校 # 2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开 # ...