I have an app that works fine in iOS 4, but there is a crash when scrolling a UITableView when the same exact code is compiled using iOS5 and XCode 4.2. The offending code is below:
- (NSString *)getDefaultIconName {
NSInteger value = [self.iconId characterAtIndex:0] % 4;
NSString *returnValue = nil;
switch (value) {
case 0:
returnValue = @"default_icon_1";
break;
case 1:
returnValue = @"default_icon_2";
break;
case 2:
returnValue = @"default_icon_3";
break;
case 3:
default:
returnValue = @"default_icon_4";
break;
}
return returnValue;
}
This method is called from within a subclass of UITableViewCell that is created or re-used in a call to cellForRowAtIndexPath. When the table is created and the cells are shown, this call returns a correct string. When I scroll down the table, this call returns an invalid reference, which causes my attempt to retain the string in another class to crash with EXEC_BAD_ACCESS. In the debugger, I can see that the UITableViewCell exists correctly and all values are set properly except for the return value for this call, which says Invalid CFStringRef.
Oddly, if I place an NSLog statement printing out the returnValue before returning, it does not crash. The same is true if I put a check to see if returnValue isKindOfClass:[NSString class] before returning it.
A third thing I noticed is that if I compile with code optimization turned off, it also does not crash.
I want to make sure i fix this correctly in the app so that the problem does not occur again in the future.
edit: Sorry, the returnValue missing a * was a typo.