views:

2121

answers:

5

I'm getting memory leak with UIImagePickerController class.

Here's how I'm using it:

 UIImagePickerController *picker = [[UIImagePickerController alloc] init];
 picker.delegate = self;
 picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
 [self presentModalViewController:picker animated:YES];
 [picker release];

To remove the picker i call [picker dismissModalViewControllerAnimated:YES]; in didFinishPickingImage and imagePickerControllerDidCancel.

--

Instruments show around 160bytes leaking as a result of this instruction:

+[UIImagePickerController _loadPhotoLibraryIfNecessary]

Apparently this issue has and is disturbing many people, and solution to avoid this problem is to build a singleton class dedicated for picking images from library or capturing using device's build in camera.

Anyone want to add something?

+1  A: 

As the author of one of the first articles about the necessity to use a singleton, the motivation was to prevent a crash on the 7/8th image capture, not because of any particular worry about the leak. 160 bytes is annoying, but not a major problem, and therefore not worth worrying about (because it can't be fixed by developers).

Airsource Ltd
I'd be interested in whether you still see the crash after 7 or 8 pictures. I tried reproducing the problem described in your post on 2.2.1 and got no crash after 20 pictures, releasing the picker after each one. It looks like either the problem's fixed or I'm doing something different than you.
Tom Harrington
I haven't tried 2.2.1 yet - I'll give that a go when I get a chance. Thanks for the heads up.
Airsource Ltd
I guess i should consider moving to 2.2.1 as well then, if some of the issues will be fixed.I just found out about this comment feature 10 minutes ago :D. Thanks guys.
Mustafa
Unfortunately we also need all the users to move to 2.2.1 as well. Until then, we have to keep testing on older versions.
Airsource Ltd
I just downloaded and ran my application using 2.2.1 and i still see leak in simulator. :( Are you doing anything different? I'm using code in iPhoneAppProgrammingGuide.pdf.
Mustafa
Tom said he gets no crash, not that he gets no leak. The problems aren't necessarily related - we don't know..
Airsource Ltd
A: 

Have you tried deleting the delegate line? I’ve had similar problems with AVAudioPlayer when delegating to self. (Even though the accessor says assign in both cases.) If the leak goes away with the delegation, you can delegate to a different object.

zoul
A: 

@Airsource Ltd: Actually in my case, user can take multiple pictures which in turn are saved and a thumbnail is retained. I create the thumbnail using undocumented _imageScaledToSize method. Do i need to worry about the crash?

What's the recommendation - afford leak or use singleton class?

@Zoul: Can you please elaborate. I didn't get what your suggesting? I can't delete the delegate line:

picker.delegate = self;

...because i need to trap -didFinishPickingImage and -imagePickerControllerDidCancel

Mustafa
Just try to delete that line for a second. If the leak goes away, than your problem was caused by delegating to self and will go away by handling the delegate events in a different class.
zoul
If you create multiple ImagePickers, then you need to worry about the crash.
Airsource Ltd
In my case, user can take 1 picture or 100 pictures, but i can't say with certainty that they will take more than 1 picture or not. Its one of the features, so i guess i should move to singleton approach. @Zoul, i will try that and will let you know. Right now i'm stuck with memory warnings :)
Mustafa
A: 

Does anyone have a good sample for the singleton approach. I tried but I still get the leak and my app crashes.

Thanks so much!!!

A: 

If you see memory leaks several GeneralBlock and SegmentMachO by using UIImagePickerController,

Try by adding CoreLocation framework and MapKit framework to your project. I don't see anymore memory leaks in the instrument tool leak checking. I don't know how UIImagePickerController related to these frameworks. I am not sure it is good solution or not. "adding frameworks without using or necessary".

I have also got the memory leak by using UIImagePickerController. That memory leak happen even in the sample code "PhotoLocation" and "iPhoneCoreDataRecipes" downloaded from developer.apple.com. I also checked by adding these frameworks to those downloaded sample code. There is no memory leaks anymore.

AKPhyo