2011-09-17 15 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セクションでその変数を解放するか、自動解放モードにしておく必要があります。 – 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];