Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImagePickerController Memory Leak

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.

like image 247
Watson Avatar asked Jul 01 '11 23:07

Watson


2 Answers

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.

like image 132
Åke Gregertsen Avatar answered Nov 19 '22 22:11

Åke Gregertsen


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.

like image 1
tc. Avatar answered Nov 19 '22 20:11

tc.