small medium large xlarge

12 Mar 2009, 00:36
Kevin Yank (4 posts)

The introduction of @dealloc@ in section 7.7 is misleading. The text instructs the reader to add this method:

- (void) dealloc {
  [greetee release];
  NSLog(@"Releasing a Greeter object.");
  [super dealloc];

At this stage of the example’s development, this code is very misleading. @greetee@ is assigned a compiler-allocated string in @init@, so releasing it is unnecessary. @Greeter@ does not yet provide a method for setting @greetee@, so this class isn’t yet retaining any new values for this field either. In short, this @dealloc@ method exists to release a value that this class will never own. It is nonsense code.

This is terribly confusing for a reader who is just learning the ins and outs of memory management for the first time. Speaking for myself, it sent me on a wild goose chase through all of my code to make sure every @@”string”@ I was creating had a matching @release@ message. As this rapidly got out of hand, I began second-guessing this requirement. Finally, I resorted to reading the official documentation on the subject, which set me straight.

It would make more sense to wait and introduce the @dealloc@ method only after section 7.10, in which @Greeter@ is enhanced to accept (and @retain@) a string value in the @initWithName@ initializer.

You must be logged in to comment