7

I saw this thing on using instruments for my app. When I profile my app, the initial memory occupied is 563 KB which is before UIImagePickerController pops up. There is one button on the first viewController which makes the UIImagePickerController appear.
As soon as UIImagePickerController appears, memory occupied goes upto 1.6 - 1.7 MB. If I select any image or cancel the UIImagePickerController, the memory occupied is still 1.6 - 1.7 MB which I believe should be 563 KB(or may be few KB's more).
Please see the below code I have used :

- (IBAction)chooseButtonPressed:(id)sender
{
    UIImagePickerController *pickerController = [[UIImagePickerController new]autorelease];
    [pickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    [pickerController setDelegate:self];
}  

Why is the memory not being released?

enter image description here

Nitish
  • 13,845
  • 28
  • 135
  • 263
  • Are you looking at the `overall bytes` occupied or the `live bytes`? For obvious reasons, `overall bytes` will never decrease... – tipycalFlow Mar 12 '12 at 07:04
  • Yes I am looking at overall bytes. But why won't it decrease? – Nitish Mar 12 '12 at 07:09
  • It's a blind sum of all the bytes there have ever been added to the memory. When memory is released, the sum is not decreased. Right on the left of `overall bytes`, there must be a column of `live bytes`. Observe the values in this column and your question will be answered! – tipycalFlow Mar 12 '12 at 07:11
  • Both **Live bytes and Overall bytes** are same. – Nitish Mar 12 '12 at 07:17
  • Actually, in xCode 4.x, `Live bytes` are two columns to the left of `Overall bytes`. Also, by `Both Live bytes and Overall bytes are same.`, did you mean their values are same as each other or same with time? – tipycalFlow Mar 12 '12 at 07:35
  • There value is same all the time. – Nitish Mar 12 '12 at 07:42
  • So did you observe `live bytes` vs. `overall bytes` ? – tipycalFlow Mar 12 '12 at 07:43
  • There is nothing to compare b/w them. They have same value. – Nitish Mar 12 '12 at 07:47
  • Check this out [go through this link.](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html) – Kamar Shad Mar 12 '12 at 06:38
  • I am tracking this problem, too. Using Xcode 4.3.2 for iOS SDK 4.3, 5.0 and 5.1. I still have this problem. One thing I notice is: if you have NO image in your photo library, it won't leak the memory (tested in Simulator). I also read the following threads without any solution yet. Let's keep tracking http://stackoverflow.com/questions/6554225/uiimagepickercontroller-memory-leak http://stackoverflow.com/questions/1447367/uiimagepickerview-controller-creating-memory-leaks-in-iphone-why http://stackoverflow.com/questions/9662639/uiimagepickercontroller-does-not-release-memory-it-occupies – Wayne Liu Apr 10 '12 at 11:48
  • Has any solution to this problem come to present? I'm having this problem, spent a lot of time trying to figure out why – TMan Feb 01 '13 at 19:57

4 Answers4

1

Since you have given it autorelease option it will get added to the autorelease pool ... see what the documentation say..

The Application Kit creates an autorelease pool on the main thread at the beginning of every cycle of the event loop, and drains it at the end, thereby releasing any autoreleased objects generated while processing an event.

you can always release the picker in the delegate call like this..

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
...
...
[picker release];

}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
....
....
[picker release];

}
Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115
1

We can't add images to comments so i'm putting this as an answer. The Live Bytes are always less than the Overall Bytes except till the first time a memory is deallocated. This can be seen from the image below.

enter image description here

I don't think there's anything wrong with your deallocation. I think you're just looking at the wrong values!

EDIT- I Think the problem might be somewhere else. To see the values I was seeing, you need to make a little change. As shown in the image below, you need to uncheck the option Only track active allocations to see the values you're looking for. If you still see 7.41 MB in Active allocations, then the problem is something else.

enter image description here

tipycalFlow
  • 7,594
  • 4
  • 34
  • 45
  • Please see my edited question. I have added a screen shot. Live Bytes is same as Overall Bytes – Nitish Mar 12 '12 at 09:08
  • @Nitish Please try the little change I've mentioned in my edit. – tipycalFlow Mar 12 '12 at 09:39
  • But how to uncheck this. Edit option is not available here. – Nitish Mar 12 '12 at 09:52
  • I got the problem. I was checking leaks and not allocations on instruments. So the values came out same for live and overall bytes. But does that make sense? – Nitish Mar 12 '12 at 09:56
  • The unchecking can happen only if the profiling is stopped. You can always start it again. I'm not sure why the live and overall bytes came out same when you were checking leaks. Probably because the `Only track active allocations` option was checked in that too. – tipycalFlow Mar 12 '12 at 10:36
0

try this

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
hchouhan02
  • 948
  • 1
  • 8
  • 18
  • no you tried autorelease instead of autorelease release memory at that time. and in `- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info` method write `[picker dismissModalViewControllerAnimated:YES];` – hchouhan02 Mar 12 '12 at 06:44
0

Did you set delegate to nil?

For more information you can refer to UIImagePickerConrtoller class reference

[picker release];
picker.delegate = nil ;

Hope this helps you.

Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
Maulik
  • 19,348
  • 14
  • 82
  • 137
  • @Maulik: I edited the answer and added class reference for UIImagePickerController as the answer appeared under `Low Quality Post`. – Parth Bhatt Mar 26 '12 at 05:58
  • @ParthBhatt: thanks... where did you found Low Quality Post ??? I don't know about it.. ;D – Maulik Mar 26 '12 at 05:59
  • 1
    @Maulik It is under the `review` section. I was reviewing the Low quality posts and found yours and hence edited it. – Parth Bhatt Mar 26 '12 at 06:01
  • @ParthBhatt: thanks again...Next time I'll take care about it... :D – Maulik Mar 26 '12 at 06:02
  • @Maulik: I have not changed your answer though as I can't completely change the answer of the poster. As it can cause him disadvantages and at times advantages in terms of reputation. :) – Parth Bhatt Mar 26 '12 at 06:04