Following this question, and more specifically, this comment:
because retain (aka strong reference) cycles in the common case where the timer's target is also its owner
I am wondering why dealloc
isn't a good place to invalidate an NSTimer
.
I remember profiling my app without auto-repeating NSTimer
invalidation and then with invalidation in dealloc
, and the memory correctly freed.
Is dealloc
working differently in the latest iOS?
Isn't in fact your overridden dealloc
called prior to any NSObject
deallocation? What is dealloc
even used for, then? If not manually deallocating the respective object's properties?
-dealloc
method is being invoked by the system when the system wants to deallocate a released object, you cannot rely on when that actually happens while the timer is still running; on the other hand you can could make accidentally a strong retain cycle when the-dealloc
will never be triggered, and your timer would be also stuck in background doing a task which you don't need to anymore, which may lead inconsistent behaviour or even crash, but definitely makes your app a bad memory citizen. – AbridgeUIViewController
'sdealloc
called after you calldismissViewController:animated:
? – Geist