This has to be very basic, but I don’t see the problem. The program crashes whenever the following code block is executed. Analyzer reports a possible memory leak:
if (anImage) {
eventImageView.frame = defaultEventImageFrame;
UIImage *scaledImage = [anImage scaleToFitWithin:defaultEventImageFrame.size interpolationQuality:kCGInterpolationHigh];
eventImageView.backgroundColor = [UIColor colorWithPatternImage:scaledImage];
}
The message is -[UIImage release]: message sent to deallocated instance 0x1129d920*
Instance 0x1129d920 is scaledImage
I tried adding retains and releases, like this
if (anImage) {
eventImageView.frame = defaultEventImageFrame;
UIImage *scaledImage = [[anImage scaleToFitWithin:defaultEventImageFrame.size interpolationQuality:kCGInterpolationHigh] retain];
eventImageView.backgroundColor = [UIColor colorWithPatternImage:scaledImage];
[scaledImage release];
}
and still get the error message.
So I tried replacing the assignment with a copy, like this
if (anImage) {
eventImageView.frame = defaultEventImageFrame;
UIImage *scaledImage = [anImage copy];
eventImageView.backgroundColor = [UIColor colorWithPatternImage:scaledImage];
}
And the problem is gone.
Checking the scaleToFitWithin method, I see it returns an autoreleased object:
- (UIImage *) scaleToFitWithin:(CGSize) newSize
interpolationQuality:(CGInterpolationQuality)quality{
CGSize originalImageSize = self.size;
CGSize newImageSize;
if (originalImageSize.width <= originalImageSize.height) {
newImageSize.width = self.size.width * newSize.width / self.size.width;
newImageSize.height = self.size.height * newSize.width / self.size.width;
}
else {
newImageSize.width = self.size.width * newSize.height / self.size.height;
newImageSize.height = self.size.height * newSize.height / self.size.height;
}
return [[[self normalize] resizedImage:newImageSize interpolationQuality:kCGInterpolationHigh] autorelease];
}
So there is something about memory management that I’m not understanding. What is the problem likely to be?
The problem is most likely that the
scaleToFitWithin:interpolationQuality:method is callingautoreleaseon an object which has already previously beenautoreleased. This can occur if you initialise theUIImageusing a temporary constructor, like+[UIImage imageWith...], earlier in the same method you call the scaling method from. The reason it works when you use[anImage copy]is because the behaviour of thecopyconstructor is such that the object returned to you has already hadretaincalled on it (so it has a local retain count of 1 and zeroautoreleases).What happens at the end of the current run loop is: the autorelease pool which is currently in use is drained, and as a part of that two
releasemessages will be sent to theUIImage. When the first one is sent, the application then runs off and callsdeallocon the image, because theretainCounthas decreased to zero. When the second one is sent, the application throws an exception because a message is being sent to an object which was previously deallocated.Try removing the
autoreleasemessage from thescaleToFitWithin:interpolationQuality:method. Even if yourresizedImage:interpolationQuality:method IS returning a new object, you should only be callingautoreleasein that method rather than the scaling method.