UIImagePickerController Memory Leak
Asked Answered
T

5

8

I am seeing a huge memory leak when using UIImagePickerController in my iPhone app. I am using standard code from the apple documents to implement the control:

    UIImagePickerController* imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        
        switch (buttonIndex) {
            case 0:
                imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
                [self presentModalViewController:imagePickerController animated:YES];
                break;
            case 1:
                imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                [self presentModalViewController:imagePickerController animated:YES];
                break;
            default:
                break;
        }
    }

And for the cancel:

-(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [[picker parentViewController] dismissModalViewControllerAnimated: YES];
    [picker release];
}

The didFinishPickingMediaWithInfo callback is just as stanard, although I do not even have to pick anything to cause the leak.

Here is what I see in instruments when all I do is open the UIImagePickerController, pick photo library, and press cancel, repeatedly. As you can see the memory keeps growing, and eventually this causes my iPhone app to slow down tremendously.

enter image description here

As you can see I opened the image picker 24 times, and each time it malloc'd 128kb which was never released. Basically 3mb out of my total 6mb is never released.

This memory stays leaked no matter what I do. Even after navigating away from the current controller, is remains the same. I have also implemented the picker control as a singleton with the same results.

Here is what I see when I drill down into those two lines:

enter image description here

Any help here would be greatly appreciated! Again, I do not even have to choose an image. All I do is present the controller, and press cancel.

Update 1

I downloaded and ran apple's example of using the UIIMagePickerController and I see the same leak happening there when running instruments (both in simulator and on the phone).

http://developer.apple.com/library/ios/#samplecode/PhotoPicker/Introduction/Intro.html%23//apple_ref/doc/uid/DTS40010196

All you have to do is hit the photo library button and hit cancel over and over, you'll see the memory keep growing.

Any ideas?

Update 2

I only see this problem when viewing the photo library. I can choose take photo, and open and close that one over and over, without a leak.

Theseus answered 1/7, 2011 at 23:52 Comment(5)
If you have NSZombieEnabled (I think it's also a checkbox in the scheme settings), that'd cause an apparent leak...Latin
What OS version/device? If it's iOS 5, report it to Apple or discuss in the appropriate dev forum.Latin
Using iOS 4.3. The phone I run on is iPhone 4.Theseus
Duplicate Question: https://mcmap.net/q/1174210/-iphone-memory-leakTrack
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 #6554725 #1447867 #9663139Thymol
S
5

It's a bug in the SDK. File a report with Apple. I have the samme isue. It is also documented here: http://www.cocoabuilder.com/archive/cocoa/285293-iphone-memory-leak-can-explain.html and that was over a year ago and still no fix.

Spur answered 5/7, 2011 at 1:50 Comment(1)
So everyone that uses the photo browser just leaks memory? It's a pretty significant amount.Theseus
L
1

A few of our apps reuse the same UIImagePickerController due to a leak in 2.x (it makes me feel old...). I was under the impression that the leak was fixed, but I could be wrong.

It's a slightly horrible workaround, but sometimes that's the best you can do.

Latin answered 2/7, 2011 at 0:30 Comment(3)
Thanks. I have tried reusing the same UIImagePickerController and I get the same issue.Theseus
I just implemented this solution and it works like a charm. Here's a link to a github project that contains an example of the solution (github.com/mikeytdan/CameraLeak/pull/1)Jaquiss
@eckyzero: The bug is still around? Wow.Latin
A
0

Try setting the UIImagePickerController.delegate to nil before releasing.

-(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [[picker parentViewController] dismissModalViewControllerAnimated: YES];
    picker.delegate = nil;
    [picker release];
}
Allround answered 2/7, 2011 at 0:12 Comment(1)
Didn't work. Still have the leak after setting the delegate to nil.Theseus
T
0

The "Mark Heap" button in Instruments has been, for me, the absolute best way of tracking down these sorts of issues.

This is an OK article on how to use it: http://www.friday.com/bbum/2010/10/17/when-is-a-leak-not-a-leak-using-heapshot-analysis-to-find-undesirable-memory-growth/

But it will tell you, for sure, which objects are surviving longer than you expect... and, ultimately, what the source of the issue is.

You can also see a complete retain/release trace for each individual object which survived - allowing you to pinpoint where your problem is.

EDIT: I use a UIImagePickerControllers as well, and I can promise it doesn't leak (at lesat for me) the way you're suggesting - so, whatever is going on, it's almost surely fixable.

Track answered 2/7, 2011 at 0:46 Comment(12)
Thanks Steve! I didn't know about "Mark Heap". Here's what I am seeing when I run Apple's photo picker example code (I see the exact same leaks when viewing the heapshots in my code). i56.tinypic.com/2yzbwd3.png as seen here developer.apple.com/library/ios/#samplecode/PhotoPicker/…Theseus
@Watson; and if you click that little arrow to the right of both the 128k and the 36k objects, what do you see for the release/retain traces?Track
Steve, for the 128k it shows 1 line of Malloc 128.00 KB Malloc 1 RefCt UIKit Responsible Library -[UINavigationController _startTransition:fromViewController:toViewController:] and the 36k is the same thing except 36k.Theseus
But if you click on each of those retain/release lines, you'll see a complete stack trace on the right-hand panel... we need both things... can u screenshot them? (I'm lazy and only willing to help vicariously through you LOL)Track
haha I appreciate your help! Been banging my head against this one all day. Is this the stack trace you are looking for? its on the extended detail on the right hand side in this screen shot: i56.tinypic.com/2yzbwd3.png and here is the 36k one: i56.tinypic.com/30ncfw4.pngTheseus
Yes, the second one is what i was looking for. The "history". One thing I forgot to mention... when using the "Mark Heap" function, you need to make sure you force everything (cache, etc) to come back to zero when you expect it to... which a simulated memory warning is perfect for. Try causing a simulated memory warning in the simulator right before you mark each heap... if the problem "goes away" then it's nothing to worry about... just cache. PS. This question is an exact DUPE of: https://mcmap.net/q/1174210/-iphone-memory-leakTrack
I tried the simulate memory warning and still no luck. Interesting he was seeing the exact same thing. I've deleted the app multiple times. I've tried it on my phone, on my ipad, on the phone simulator, on the ipad simulator, all the same result. Even running the apple photo picker example project i see the exact same result with the heap shots. Guess I can try building from a different computer just for the hell of it? Really at a loss here.Theseus
Tried on a different computer running 4.2 and still seeing the same thing. I got it from 2mb all the way up to 16ish mb from just opening and canceling the uiimagepickercontroller. Sad thing is I cant even think of a work around for this.Theseus
I'm shocked the singleton instance wasn't helping the situation... are you sure your attempted implementation was kosher?Track
Yeah I followed Apple's guidelines to the T. Take a look at this i51.tinypic.com/21on1bm.png . I downloaded and ran Apple's example for image picker and I see the same results. developer.apple.com/library/ios/#samplecode/PhotoPicker/… . What gives?Theseus
Does it happen when you run the app on the actual device?Track
Yeah the only difference is the Responsible Caller on the simulator is ReadITImageDB from library MusicLibrary, and on the device it shows as from UINavigationController _startTransition etc from UIKit. The same amount is leaked with the exact same actions simulator and device.Theseus
L
0

I used UIImagePickerController and after 40 capture images my application received a DidMemoryWarning message and stop, hidden all my views.

In my application I create 40 objects of

UIImagePickerController( new UIImagePickerController() )

To work correctly I create a unique instance shared to all application and with this all work correctly.

I supusose that control lost memory too, but only one time. My application can capture images from camera correctly:

private static UIImagePickerController picker = new UIImagePickerController();
Loritalorn answered 18/12, 2012 at 10:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.