So far I have been removing observers (notifications or KVO) in the dealloc. Since dealloc is gone in ARC, what's the recommended way to do this?
Asked
Active
Viewed 2,475 times
4

Peter Hosey
- 95,783
- 15
- 211
- 370

cfischer
- 24,452
- 37
- 131
- 214
-
2Note that [it’s usually better if you can send -removeObserver:… before -dealloc gets called](http://stackoverflow.com/questions/13927/in-cocoa-do-i-need-to-remove-an-object-from-receiving-kvo-notifications-when-dea/14054#14054). – Oct 20 '11 at 21:03
1 Answers
17
-dealloc is not gone under ARC. The ivar-releasing and super-calling aspects are handled automatically, allowing you to omit it if that's all you were going to do, but you should still implement it for other things if it makes sense to do so.

Catfish_Man
- 41,261
- 11
- 67
- 84
-
3Yep, iOS 5.0 documentation says "If you deallocate an object that is observing notifications, you need to tell the notification center to stop sending it notifications." so -dealloc method will continue to be needed. – chuckSaldana Oct 19 '11 at 21:04
-
@Catfish_Man Are you sure dealloc still gets called? I made some tests and mine never gets called. – cfischer Oct 20 '11 at 20:31
-
4Yes, I'm sure. See http://clang.llvm.org/docs/AutomaticReferenceCounting.html#misc.special_methods.dealloc – Catfish_Man Oct 20 '11 at 23:21
-
But in the same place (http://clang.llvm.org/docs/AutomaticReferenceCounting.html#misc.special_methods.dealloc) I also read: "Rationale: even though ARC destroys instance variables automatically, there are still legitimate reasons to write a dealloc method, such as freeing non-retainable resources. Failing to call [super dealloc] in such a method is nearly always a bug. " – Elise van Looij Nov 30 '11 at 15:22
-
1If I set the object *posting* the notifications to `nil`, do I need to remove observers watching it, or is that cleaned up automatically by ARC? – jowie Jul 16 '12 at 14:41
-
-
-