Interesting autorelease behavior


#1

This possibly should be living in memory management…
I was wrestling with why my table view would crash when scrolled (after loading fine) - as cells went off screen it would crash immediately after calling cellForRowAtIndexPath.
This would not happen if I just loaded the table with numbers ([indexPath row]), but only when going back to the the Possessions description method, whether I loaded possessions into the table or not.
It turned out that I had called autorelease twice:
Possession *newPossession = [[[self alloc] initWithPossessionName:randomName serialNumber:randomSerialNumber valueInDollars:randomValue] autorelease];
return [newPossession autorelease];
You can have it in one or the other but not both. A rather niche problem, but presumably the objects in the array had disappeared, causing a perfectly reasonable crash. And of course, it didn’t bother the original RandomPossessions program at all.
A usefull lesson in how the problem may be somewhat removed from where you actually are