Question on the dealloc method and memory leaking


#1

I noticed that in the first example of this Chapter (chapter 21) that in the class Asset we override the dealloc method inherited from this classes super NSObject (see Asset.m page 126 in my book).

We do this by declaring our own version of dealloc, and by doing so we use NSLog to output a message onto the screen. This message gives us indication that the dealloc method has been called.

This method however does nothing more in terms of actually deallocating the Object.

My question is: Would this not cause memory leaks to occur, because we have actually broken the chain of events that do the actual deallocation?

All comments welcome

Update Oct 28, 2012:
Sorry everyone for this post, it actually belongs in the discussion for Chapter 20 not 21. I will repost it there.


#2

No, if you are not using ARC and as long as all objects invoke superclass’s dealloc:

- (void)dealloc
{
   // clean up
   ...
   [super dealloc]
}

Since NSObject sits at the top of the class hierarchy, it will do the right thing to release the memory allocated for the current object, self.

If you are using ARC, you only do this:

- (void)dealloc
{
   // clean up, including what ARC can't
   ...
}

#3

Sorry ibex10 I submitted this question to the wrong chapter, its actually meant for chapter 20. I reposted it there.

Does your answer still apply, given the code I am referring to on pg 126 for the dealloc method of Asset.m ?

Sorry I’ll try to be more careful next time.