UIImagePNGRepresentation Leaking memory

1.8k views Asked by At

I have used below line in my code

  NSData *dataForImage=UIImagePNGRepresentation(image);

My app crashes due low memory.When I use VM tracker/Allocation it shows me heap growth. I analyzed the code. It shows me leak on above line as 100%. Is UIImagePNGRepresentation method takes more memory allocation? As I know, it returns an autoreleased object.

Please let me know if I am doing anything wrong.

EDIT: Its not showing leak in leak instrument. I used allocation instrument and then taken heapshot for particular functionality. It shows me heap growth. I think it is responsible for app crash after more use of app.In heapshot I used "Extended details", it shows me 100% on above line

EDIT: Hope this helps

-(IBAction)Save{

                MyInfo *myInfo = [[MyInfo alloc]init];
                if (theimageView.image != nil) {

                     UIImage *image=[theimageView.image fixImageOrientation];
                    [myInfo set_image:image];
                    NSData *dataForImage=UIImagePNGRepresentation(image);
                    image=nil;

                    [myInfo set_imageData:dataForImage];
                    dataForImage=nil;
                }
  [tempArray addObject:myInfo];
  [myInfo release];
  myInfo=nil;
  // Here I have added myInfo object in array and released that object


 }

In the above code, fixImageOrientation is a category written for UIImage.

1

There are 1 answers

0
RobCroll On

My guess is its the UIImagePNGRepresentation(image) method that is causing the problem. You need to drain the pool.

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSData *dataForImage=UIImagePNGRepresentation(image);

[pool release];

or in ARC

@autoreleasepool
{
    NSData *dataForImage=UIImagePNGRepresentation(image);
}