【问题标题】:UIImage memory leakUIImage 内存泄漏
【发布时间】:2011-09-17 06:32:54
【问题描述】:

谁能解决我在以下代码中的内存泄漏问题???

-(void)paint:(ImageWarper::WarpedImage *)warpedImg isCircleRequired:(bool)doDrawCircle atPoint:(CGPoint)pt{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if(!mWarper) 
    return;
unsigned char *pixelData = warpedImg->Image.Data;
int imageHeight  = warpedImg->Image.Height;
int scanWidth = warpedImg->Image.ScanWidth;
int imageWidth = warpedImg->Image.Width;
CGDataProviderRef provider = CGDataProviderCreateWithData(
                                                          NULL, 
                                                          pixelData, 
                                                          imageHeight * scanWidth, 
                                                          NULL);

CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
int bytesPerPixel = warpedImg->Image.Bpp;
CGImageRef imageRef = CGImageCreate(imageWidth,
                                    imageHeight,
                                    BitsPerComponent,
                                    bytesPerPixel * BitsPerComponent,
                                    scanWidth,
                                    colorSpaceRef,
                                    bitmapInfo,
                                    provider,
                                    NULL,
                                    YES,
                                    renderingIntent);

UIImage *uiImage = [UIImage imageWithCGImage:imageRef];
CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(provider);
//  CGColorSpaceRelease(colorSpaceRef);
CGImageRelease(imageRef);
imgScrollView.imgView.image = uiImage;
UIGraphicsBeginImageContext(mbmpImage.size); 
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ctx, 1.5);
CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);

[mbmpImage drawInRect:CGRectMake(0, 0, mbmpImage.size.width, mbmpImage.size.height)]; 
[uiImage drawInRect:CGRectMake(warpedImg->Position.X, warpedImg->Position.Y, warpedImg->Image.Width, warpedImg->Image.Height)];         
[mbmpImage release];
mbmpImage = [UIGraphicsGetImageFromCurrentImageContext() retain];
if(doDrawCircle){
    mbmpImage = [UIGraphicsGetImageFromCurrentImageContext() retain];           
    CGContextStrokeEllipseInRect(ctx,CGRectMake(pt.x - mRadius, pt.y - mRadius, mRadius*2, mRadius*2));
}
UIImage * resultingImage = [UIGraphicsGetImageFromCurrentImageContext() retain];    

UIGraphicsEndImageContext();
imgScrollView.imgView.image =  resultingImage ;
if(!doDrawCircle)       
    mbmpImage = [resultingImage retain];

[resultingImage release];
[pool drain];
}

这个函数在触摸事件上被调用..

这是显示泄漏的仪器快照..

【问题讨论】:

  • 也许您只是传递保留消息并将这些值提供给 mbmpImage,但从未释放 mbmpImage...尝试适当地释放它。
  • mbmpImage 是类变量,我到处都在使用它
  • 那么您应该在 dealloc 部分释放该变量,或者将其保持在自动释放模式。
  • 是的,我在 dealloc 中发布它
  • 好的,虽然不是一个完全可靠的解决方案,但请尝试在上述方法和 dealloc 部分检查该变量的保留计数,以了解它可能泄漏的位置。

标签: iphone memory-leaks uiimage


【解决方案1】:

替换

[mbmpImage release];
mbmpImage = [UIGraphicsGetImageFromCurrentImageContext() retain];
if(doDrawCircle){
    mbmpImage = [UIGraphicsGetImageFromCurrentImageContext() retain];           
    CGContextStrokeEllipseInRect(ctx,CGRectMake(pt.x - mRadius, pt.y - mRadius, mRadius*2, mRadius*2));
}
UIImage * resultingImage = [UIGraphicsGetImageFromCurrentImageContext() retain];    

UIGraphicsEndImageContext();
imgScrollView.imgView.image =  resultingImage ;
if(!doDrawCircle)       
    mbmpImage = [resultingImage retain];

[resultingImage release];
[pool drain];

[mbmpImage release];
mbmpImage = [UIGraphicsGetImageFromCurrentImageContext() retain];
if(doDrawCircle)
    CGContextStrokeEllipseInRect(ctx,CGRectMake(pt.x - mRadius, pt.y - mRadius, mRadius*2, mRadius*2));
UIImage * resultingImage = [UIGraphicsGetImageFromCurrentImageContext() retain];    

UIGraphicsEndImageContext();
imgScrollView.imgView.image =  resultingImage ;
if(!doDrawCircle)
{
    [mbmpImage release];
    mbmpImage = [resultingImage retain];
}

[resultingImage release];
[pool drain];

【讨论】:

    猜你喜欢
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多