Xcode Analyze complained that I incorrectly decremented ref count at the line marked “this line”. it seems a bit strange, since it was not obvious that that line decrements the ref count.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
UIImage * image = [[UIImage alloc] initWithData:receivedData];
if (image == nil) {
image = [UIImage imageNamed:@"null.bmp"];
}
self.itemImage.image = image; //this line
self.promotion.image = image;
[image release];
}
This is a mildly tricky thing; it’s understandable that it is causing confusion. The two paths here result in
imageholding objects with different ownership statuses, and thus different reference counts.Going through the
ifresults inimageholding an object that your code doesn’t own.I think that the analyzer is indicating the wrong line, in the same way the compiler will complain about a missing semicolon five lines later.
The way to solve this may be to retain the image you get from
imageNamed:You can’t just not sendrelease, because you do ownimagein one case, and you need to properly relinquish that ownership. I’d also suggest putting a comment in there about sendingretainso you remember eight months later why you did it.Better than that, as suggested below by the inimitable Bavarious, and as I think you yourself already figured out, would be to autorelease the
alloc‘d image and remove the laterreleaseline.