iOS常见的不调用dealloc的原因

(1)NSTimer

如果代码中存在NSTimer,当调用以下代码的时候,会增加ViewController的return count,需要在关闭页面的时候调用[timer invalidate] 销毁计时器

[NSTimer scheduledTimerWithTimeInterval:1.0
                                 target:self
                               selector:@selector(updateTime:)
                               userInfo:nil
                                repeats:YES];

(2)Block

Block体内使用实例变量也会造成循环引用,使得拥有这个实例的对象不能释放。 例如你这个类叫ViewController,有个属性是NSString *name; 如果你在block体中使用了self.name,那样子的话这个类就没法释放。 

要解决这个问题只需

__weak typeof(self) weakself = self;

(3)Delegate

在声明delegate的时候需要设置成weak,如果是strong强引用,就不会调用dealloc。


@property (nonatomic,weak)id<UIRefershCollectionViewDelegate> refreshDelegate;

猜你喜欢

转载自blog.csdn.net/watson2017/article/details/86571283