I've had a very rarely occurring bug where a key in an NSMutableDictionary
instance ends up pointing to the wrong object (another object in the same dictionary).
Could "key confusion" be the result of a race condition?
Here's the code (I've put in new thread safety since):
- (NSNumber*) makeHashFromResizedImage:(UIImage*)original newSize:(CGSize)newSize {
int retVal = original.hash + newSize.width * 2 + newSize.height * 4;
return [NSNumber numberWithInteger:retVal];
}
- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize {
NSNumber *key = [self makeHashFromResizedImage:image newSize:newSize];
if ([self.resizedImages objectForKey:key]) {
return [self.resizedImages objectForKey:key];
}
UIImage *newImage = {{image-resizing-code-here}};
[self.resizedImages setObject:newImage forKey:key];
return newImage;
}
Note: {{image-resizing-code-here}}
was removed for brevity. I'm beginning to suspect my makeHashFromResizedImage
as it probably doesn't work as expected. Going to Unit Test it now.