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.

Community
  • 1
  • 1
Watson
  • 286
  • 2
  • 7
  • 1
    If you have NSZombieEnabled (I think it's also a checkbox in the scheme settings), that'd cause an apparent leak... – tc. Jul 02 '11 at 00:27
  • What OS version/device? If it's iOS 5, report it to Apple or discuss in the appropriate dev forum. – tc. Jul 02 '11 at 00:43
  • Using iOS 4.3. The phone I run on is iPhone 4. – Watson Jul 02 '11 at 00:46
  • Duplicate Question: http://stackoverflow.com/questions/4418518/iphone-memory-leak – Steve Jul 02 '11 at 05:12
  • 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:49

5 Answers5

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.

Åke Gregertsen
  • 185
  • 2
  • 7
  • 3
    So everyone that uses the photo browser just leaks memory? It's a pretty significant amount. – Watson Jul 06 '11 at 14:18
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.

tc.
  • 33,468
  • 5
  • 78
  • 96
  • Thanks. I have tried reusing the same UIImagePickerController and I get the same issue. – Watson Jul 02 '11 at 00:34
  • 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 (https://github.com/mikeytdan/CameraLeak/pull/1) – ebandersen Oct 07 '13 at 20:30
  • @eckyzero: The bug is *still* around? Wow. – tc. Oct 20 '13 at 14:04
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();
Tim Post
  • 33,371
  • 15
  • 110
  • 174
0

Try setting the UIImagePickerController.delegate to nil before releasing.

-(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [[picker parentViewController] dismissModalViewControllerAnimated: YES];
    picker.delegate = nil;
    [picker release];
}
Till
  • 27,559
  • 13
  • 88
  • 122
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.

Steve
  • 31,144
  • 19
  • 99
  • 122
  • 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). http://i56.tinypic.com/2yzbwd3.png as seen here http://developer.apple.com/library/ios/#samplecode/PhotoPicker/Introduction/Intro.html%23//apple_ref/doc/uid/DTS40010196-Intro-DontLinkElementID_2 – Watson Jul 02 '11 at 01:40
  • @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? – Steve Jul 02 '11 at 02:36
  • 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. – Watson Jul 02 '11 at 03:09
  • 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) – Steve Jul 02 '11 at 04:54
  • 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: http://i56.tinypic.com/2yzbwd3.png and here is the 36k one: http://i56.tinypic.com/30ncfw4.png – Watson Jul 02 '11 at 05:00
  • 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: http://stackoverflow.com/questions/4418518/iphone-memory-leak – Steve Jul 02 '11 at 05:11
  • 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. – Watson Jul 02 '11 at 05:26
  • 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. – Watson Jul 02 '11 at 05:49
  • I'm shocked the singleton instance wasn't helping the situation... are you sure your attempted implementation was kosher? – Steve Jul 02 '11 at 05:51
  • Yeah I followed Apple's guidelines to the T. Take a look at this http://i51.tinypic.com/21on1bm.png . I downloaded and ran Apple's example for image picker and I see the same results. http://developer.apple.com/library/ios/#samplecode/PhotoPicker/Introduction/Intro.html%23//apple_ref/doc/uid/DTS40010196-Intro-DontLinkElementID_2 . What gives? – Watson Jul 02 '11 at 05:58
  • Does it happen when you run the app on the actual device? – Steve Jul 02 '11 at 06:05
  • 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. – Watson Jul 02 '11 at 06:10