简体   繁体   中英

UIImage setImage: cause memory leak

I got a memory leaking problem from a very simple function. Here is my code:

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


  }

The instruments report as below:

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: method retained your newImage . To avoid memory leak you need to release newImage after setImage

   UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
   UIGraphicsEndImageContext();
   [gameView setImage:newImage];
   [newImage release];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM