简体   繁体   中英

[pool release]; Crash my app

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.

    UIImage *tmp = [image rotate:UIImageOrientationRight];
    //do some stuff with this image
    [tmp release]; // Here

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 -release is not sent until it gets drained and sends the correct release call to your object that was previously and wrongly released by you.

Probably you're releasing some objects you create and don't own between the time you create the pool and release it again.

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSString *s = [NSString stringWithFormat:@"%d", 2];
// Your string now has a retain count of one, but it's autoreleased. So when the pool
// gets released it'll release the string

[s release];
// You decrease the retain count to zero, so the object gets destroyed
// s now points to a deallocated object

[pool release];
// The pool gets destroyed, so it tries to send a release method to your string. However,
// the string doesn't exist anymore so an error occurs.

我认为您的崩溃可能与自动释放池何时释放UIImage有关,而不是与释放自动释放池有关。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM