已关注 this question ,更具体地说,this comment :
because retain (aka strong reference) cycles in the common case where the timer's target is also its owner
我想知道为什么 dealloc
不是使 NSTimer
失效的好地方。
我记得在分析我的应用程序时没有自动重复 NSTimer
失效,然后在 dealloc
中失效,并且正确释放了内存。
在最新的 iOS 中,dealloc
的工作方式是否有所不同?
实际上,您的重写的 dealloc
不是在任何 NSObject
释放之前调用吗?那么,dealloc 到底有什么用呢?如果不手动释放相应对象的属性?
最佳答案
当没有强
引用指向该对象(没有人保留)时,ARC只会释放(并调用dealloc
)对象。
NSTimer
创建strong
引用,并将保留target
。
这意味着,dealloc
将不会被调用,因为 NSTimer 仍然具有对该对象的strong
引用。如果没有dealloc,这意味着NSTimer永远不会失效……导致内存泄漏甚至崩溃。
有一种方法可以在 dealloc
中或当 target
变为 nil 时使计时器无效
。看看answer here .
关于ios - 在 dealloc 中使 NSTimer 失效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40358646/