Overriding dealloc


Hi there! I’d be most grateful if someone can clarify the following for me.

From what I understand, if a method is called that is the method that will be used. However, if this method is overridden, the overridden method is the one that will be called and not the original method. Given this, I’m mystified by the following code on page 118 of the Objective-C Programming book:

[quote]- (void) dealloc
NSLog(@“deallocating %@”, self);

Here we are obviously overriding dealloc but what I fail to see is how we are actually deallocating. All I see is a message being sent to the log. Surely, from the above code we are not deallocating, are we?

Many thanks in advance!


This is an unfortunate consequence of ARC being sneaky.

Under ARC, it’s forbidden to send the -retain, -release, -autorelease, or -dealloc messages to any object.

The compiler adds calls to these methods in all the appropriate places for you every time you build your project.

One of the calls that it inserts for you is a dealloc call:

- (void)dealloc { NSLog(@"deallocating %@", self); [super dealloc]; // You don't type this (under ARC), but it gets added for you when you compile. }

This means that after you’ve done your own teardown code, your object tells itself to use its superclass’ implementation of -dealloc to continue the process. Which in turn hands off the baton to its superclass’ implementation, until you end up using NSObject’s implementation at the very end which actually does the deallocation work.



Thank you very much for your quick response and help. I’m most grateful. Thank you! Wishing you all here the most lovely day.