- (void)setEntryDate:(NSDate *)date -> Retain Release Why?


Page 66/67
Why do you use this?

- (void)setEntryDate:(NSDate *)date{ [date retain]; [entryDate release]; entryDate = date; }
It does not get called since it’s being handled by

- (id)initWithEntryDate:(NSDate *)theDate{ self = [super init]; if(self){ entryDate = [theDate retain]; firstNumber = ((int)random() %100+1); secondNumber = ((int)random() %100+1); } return self; }.
And it’s getting released by dealloc.

- (void)dealloc{ NSLog(@"deallocating %@",self); [entryDate release]; [super dealloc]; }


Hi Kuebeker,

Perhaps I fully misunderstood the memory management problem, but a couple of pages before the ask you to insert these commands in your lottery code, they explain why. I can tell you what page it is in my ebook, but it sure isn’t on the same page as your book. However, it is the text right under Figure 4.5.

They first retain date to make sure that when entryDate and date are the same, they don’t release entryDate and date with the command [release entryDate]. That’s why they first increase the retain count by one and then release entryDate. Then they set both instances to point to the same address and the method is returned. As far as I understand returning the method decreases the count again, while both point to the same address.

However, they also specify a different way of changing entryDate and that is by first checking if both are the same or not. If both are not the same entryDate is released, but date not retained. If both are the same, the method returns without doing anything. Why they don’t ask us to put in this different, in my humble opinion better, option, I don’t know. Perhaps the argument that the checking before changing is extra overhead. This, however, is guessing from my side and I’m still learning as well.