[personName release] question , page 126


Can you please explain me the reason for releasing personName in dealloc (page 126)?


Hi there.

The “retain” counter is increased when you create the instance (on init method) and it’s responsibility of who’s retaining it to release it, otherwise there will always be a pointer to that personName instance and it will never get “garbage collected”.


Thank you for the prompt reply, but:
The retain counter is not increased in init, it even not initialized on init because we just assign static string. Try to do following:

    NSLog(@"%s(%d) retainCount:%lu", __FUNCTION__, __LINE__, [name retainCount]);
    name = @"NewName";
    NSLog(@"%s(%d) retainCount:%lu", __FUNCTION__, __LINE__, [name retainCount]);

I guess the retain counter initialized and increased only when “magical” KVC binding happen. For example you edit name cell in table view.IMHO this is a little confusing and should be clearly explained in the chapter.