Finalize not being called?


On page 380 we implement:

[code]//log during finalize

  • (void)finalize
    NSLog(@“finalizing %@”, self);
    [super finalize];

Then on page 382 Aaron tells us that when we remove polynomials we “should see log statements from the finalize method of the instance that is being deallocated.” I’m not seeing any log statements from there, just a bunch of “drawing” logs from the drawInRect method:

2010-05-25 16:48:14.683 Polynomials[49538:a0f] drawing 2010-05-25 16:48:14.863 Polynomials[49538:a0f] drawing 2010-05-25 16:48:14.864 Polynomials[49538:a0f] drawing 2010-05-25 16:48:14.865 Polynomials[49538:a0f] drawing 2010-05-25 16:48:15.038 Polynomials[49538:a0f] drawing 2010-05-25 16:48:15.039 Polynomials[49538:a0f] drawing

Is it more likely that I set something up incorrectly – or that things work differently now than when the book was written? I’m running XCode 3.2.2 on OS X 10.6.3.



Actually, it is difficult to guess when finalize will get called. Often the garbage collector will wait until memory usage surpasses some mark before it actually does a collection. I’m sorry if the book misled you on this issue.


Oh ok - thanks Aaron! I played around with it a little more, created about a hundred polynomials, removed them (repeat), and I never saw it call finalize. Then I checked using Instruments as you explain later in the chapter and I saw that the polynomial objects actually were never deallocating properly, so I figured I surely did something wrong. I went back through the chapter to ensure I hadn’t missed anything - and sure enough - I had only enabled garbage collection for the Release Configuration – oops!

Thanks for your help!