Are overriding or building on dealloc?

In both BNRAsset and BNREmployee, we added the following to the .m file:

- (void)dealloc { NSLog(@"deallocating %@", self): } "

I notice that we are not returning anything. My question is: are we overriding dealloc? I am guessing not, because we are told that we are indeed freeing up memory. But we didn’t add any code in the above to do that – just added a NSLog command.

If we had returned something, then we would we have overridden dealloc? We returned something in the overriding example from Chapter 20.

The dealloc method allows an object to do any necessary clean up before its memory is released. You are building on the dealloc process for the object.

You have, indeed, overridden dealloc here.

It’s worth noting that, as Brian says, -dealloc is not teh method that actually frees your object’s memory. It merely is a place that your object can do any last-minute cleanup prior to its actual deallocation. The method is badly named, as it leads one to think that -dealloc is actually responsible for the deallocation itself.

dealloc is confusing for an additional reason, though, and that is that sometimes when you override a method, you should call through to your superclass’ implementation of the method. Dealloc, however, is special. It’s important for the superclass’ implementation to also be called, but ARC forbids us to actually call [super dealloc] because dealloc is a forbidden message for us to send to any object on our own- ARC handles all calls to -dealloc on any object. What happens is, since we can’t call -dealloc on anything, the compiler quietly inserts a call to [super dealloc]; at the end of your own -dealloc method implementation when you build and run. You never see it there, but it’s there.

This fact causes some unease in the community, as you might guess. No one likes hidden behavior, because hidden behavior is difficult to discover for one’s self. Fortunately, it’s rare, and this is one of the rare cases.

Very helpful comments. Thanks guys