In my app i have a loop that move on array of UIImage and make stuff with this images.
the loop work in the background Thread so in the start of the function i put :
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
and in the end
[pool release];
In the loop i create UIImage so i need to release it because it give me a memory warning if i am not make a release.
When the app finish the loop and get to the
[pool release];
it give me BAD_ACCESS error and crash the app.
Edit
This is the methods in the loop
UIImage *tmp = [image rotate:UIImageOrientationRight];
//do some stuff with this image
[tmp release];
This is the rotate method:
UIImage* copy = nil;
CGRect bnds = CGRectZero;
UIImage* copy = nil;
CGContextRef ctxt = nil;
CGImageRef imag = self.CGImage;
CGRect rect = CGRectZero;
CGAffineTransform tran = CGAffineTransformIdentity;
rect.size.width = CGImageGetWidth(imag);
rect.size.height = CGImageGetHeight(imag);
bnds = rect;
UIGraphicsBeginImageContext(bnds.size);
ctxt = UIGraphicsGetCurrentContext();
switch (orient)
{
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
CGContextScaleCTM(ctxt, -1.0, 1.0);
CGContextTranslateCTM(ctxt, -rect.size.height, 0.0);
break;
default:
CGContextScaleCTM(ctxt, 1.0, -1.0);
CGContextTranslateCTM(ctxt, 0.0, -rect.size.height);
break;
}
CGContextConcatCTM(ctxt, tran);
CGContextDrawImage(UIGraphicsGetCurrentContext(), rect, imag);
copy = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if (imag) {
CFRelease(imag);
}
return copy;
You’re over-releasing your image after rotating it.
UIGraphicsGetImageFromCurrentImageContext()returns an autoreleased object, so you don’t need to call release on it after you return it.The crash happens when releasing the NSAutoreleasePool because the last
-releaseis not sent until it gets drained and sends the correct release call to your object that was previously and wrongly released by you.