1

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.

Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
user605003
  • 75
  • 1
  • 6
  • 1
    Is it really showing you leaks on that line (using the Leaks instrument), or is it just showing you allocations (in the Allocations instrument)? Either way, you need to show us all of the code that uses `dataForImage`. – rob mayoff Aug 08 '12 at 06:50
  • 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 – user605003 Aug 08 '12 at 06:52
  • You still need to edit your question to include all of the code that uses `dataForImage`. – rob mayoff Aug 08 '12 at 06:53
  • No rob... I am not using ARC...My app takes photo from camera and while saving that I called above save method – user605003 Aug 08 '12 at 07:29
  • Well, you say that you add `myInfo` to an array and then release `myInfo`. You need to show us that code, and you need to show us the code that removes the `MyInfo` object from the array. – rob mayoff Aug 08 '12 at 07:38
  • also, what kind of objects are being shown as leaked? – nielsbot Aug 08 '12 at 07:39
  • Have you tried using the static analyzer? It's the Analyze item in the Product menu. If it points out any problems, fix them. – rob mayoff Aug 08 '12 at 09:12
  • what you will do with tempArray? tempArray storing "leaked" image – NeverBe Aug 08 '12 at 09:34
  • I have stored that data later in entity and then local DB. – user605003 Aug 09 '12 at 05:53
  • Upvoted. Any fix found for this issue? – clearwater82 Jan 10 '13 at 22:30

1 Answers1

2

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);
}
RobCroll
  • 2,571
  • 1
  • 26
  • 36