2011-09-17 60 views
0

任何一個可以解決我的問題在下面的代碼中的內存泄漏?UIImage內存泄漏

-(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]; 
} 

該功能被稱爲觸摸事件..

這是儀器的卡在那裏它表明泄漏..

+0

也許你只是傳遞保留消息,並將這些值給予mbmpImage,但從不釋放mbmpImage ...嘗試正確釋放它。 – mayuur

+0

mbmpImage是類變量,我使用它在每個地方 – DivineDesert

+0

那麼你應該釋放該變量dealloc部分或保持它在autorelease模式。 – mayuur

回答

2

更換

[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];