[池发布];崩溃我的应用程序

时间:2012-01-16 16:37:37

标签: iphone objective-c

在我的应用程序中,我有一个在UIImage数组上移动的循环,并使用此图像制作内容。 循环工作在后台线程所以在我放的函数的开头:

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

最后

[pool release];

在循环中我创建UIImage所以我需要释放它,因为如果我没有发布,它会给我一个内存警告。

当应用完成循环并进入

[pool release];

它给我BAD_ACCESS错误并导致应用崩溃。

修改


这是循环中的方法

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

这是旋转方法:

    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;

3 个答案:

答案 0 :(得分:4)

旋转后你的图像过度释放。

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

UIGraphicsGetImageFromCurrentImageContext()返回一个自动释放的对象,因此您在返回后不需要在其上调用release。

在释放NSAutoreleasePool时会发生崩溃,因为最后-release在被耗尽之前不会被发送,并且会向您之前和错误释放的对象发送正确的释放调用。

答案 1 :(得分:1)

可能你发布了一些你创建的对象,并且在创建池和重新发布它之间没有拥有它们。

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.

答案 2 :(得分:0)

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