我通过一个非常简单的函数遇到了内存泄漏问题。这是我的代码:
-(void) drawPuzzle
{
gameView.image=nil;
UIGraphicsBeginImageContext(gameView.frame.size);
[gameView.image drawInRect:CGRectMake(0,
0,
gameView.frame.size.width,
gameView.frame.size.height)];
CGContextRef con=UIGraphicsGetCurrentContext();
CGContextMoveToPoint(con, 20,0);
CGContextAddLineToPoint(con,20, gameView.frame.size.height);
CGContextStrokePath(con);
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[gameView setImage:newImage];
}
仪器报告如下:
Event Type RefCt Responsible Caller
Malloc 1 +[UIImage imageWithCGImage: scale: orientation:]
Autorelease +[UIImage imageWithCGImage: scale: orientation:]
Retain 2 -[UIImageView setImage:]
Release 1 -[NSAutoreleasePool drain]
最佳答案
setImage:
方法保留了您的newImage
。为了避免内存泄漏,您需要在newImage
之后释放setImage
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[gameView setImage:newImage];
[newImage release];