Deallocation of Unretained?


#1

It appears the following retain and release statements are no longer just good practice but are required.

-(void)setEntryDate:(NSCalendarDate *)date { [date retain]; [entryDate release]; entryDate = date; }

If you don’t add that code you will get an error:
Program received signal: “EXC_BAD_ACCESS”.

Oddly when I run the downloaded example code the dealloc function is never called in LotteryEntry.m. At least it never prints out that it is deallocating even though the example in the book shows it doing so and it should be. I’ve copied the code line for line and have the garbage collector turned off. I’m guessing it is some kind of compile setting in the downloaded project file.

Again, adding the retain and release code above, which is suggested in the book, fixes the problem.


#2

[quote=“PicklePumpers”]It appears the following retain and release statements are no longer just good practice but are required.

-(void)setEntryDate:(NSCalendarDate *)date { [date retain]; [entryDate release]; entryDate = date; }

If you don’t add that code you will get an error:
Program received signal: “EXC_BAD_ACCESS”.
[/quote]

hm that’s strange, IMHO without retain/release (you can read more about this “idiom” on Page 75, Living with Retain Counts) it shouldn’t crash and just leak some memory…

[quote=“PicklePumpers”]
Oddly when I run the downloaded example code the dealloc function is never called in LotteryEntry.m. At least it never prints out that it is deallocating even though the example in the book shows it doing so and it should be. I’ve copied the code line for line and have the garbage collector turned off. I’m guessing it is some kind of compile setting in the downloaded project file.

Again, adding the retain and release code above, which is suggested in the book, fixes the problem.[/quote]

Did you clean your project after turning it off? You could also use some code like that on Page 68, Living with the Garbage Collector:

NSLog(@"GC = %@", [NSGarbageCollector defaultCollector]);

to check if GC is really off…


#3

I had the exact same problem. It crashed at the end (on page 71) until I added the retain/release (as seen on page 77). I spent 20 minutes trying to figure it out without reading ahead; I figured it out; then proceeded to read the next 6 pages where it finally mentions to the retain/release for setEntryDate:. Needless to say, my output didn’t match with Figure 4.5 on page 71 following the steps presented up until that moment, so I backtracked trying to figure it out what went wrong; only to figure out afterward that it wasn’t my fault anyway. // Xcode 3.2.3