uiimagepickerview controller creating memory leaks in iphone - why?
Asked Answered
O

4

1

uiimagepickerview controller creating memory leaks in iphone - why?

Try to implement ui image picker view controller in your application & debug it. You will find memory leaks in your application. Why ui image picker view controller creates memory leaks.


-(void)addPhotos:(id)sender
{
    if(imagePickerController==nil){ 
          imagePickerController=[[UIImagePickerController alloc]init];
          imagePickerController.delegate=self;
          imagePickerController.sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        imagePickerController.allowsImageEditing=YES;
          imagePickerController.navigationBar.barStyle=UIBarStyleBlackOpaque;
    }
[self.navigationController presentModalViewController:imagePickerController animated:YES];
}

dealloc of my view controller.


- (void)dealloc {
if(PhotoDateArray!=nil)[PhotoDateArray release];
if(imagePickerController!=nil) [imagePickerController release];
if(objDetail!=nil) [objDetail release];
if(Picimage!=nil) [Picimage release];
if(mySavePhotoController!=nil) [mySavePhotoController release];
if(LoadingAlert!=nil);
[super dealloc];
}

Video link explaining how I am getting the memory leak in it..

http://www.yourfilelink.com/get.php?fid=508534

Odd answered 19/9, 2009 at 0:51 Comment(3)
Can you post your code in how you call the uiimagepickerviewScorbutic
please watch the video. It will make the clarification. I am just editing the question nowOdd
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 #9663139Beckford
L
2

Even though you have the nil check, it's still possible to leak memory. I think what is happening here is that you are calling alloc / init multiple times, but only releasing once. My guess it that addPhoto: is wired up to some button click, dealloc would only be called once when the delegate is trying to destroy. This creates a situation like this:

  • button click
    • alloc / init
  • button click
    • alloc / init (memory leak on first alloc'd picker)
  • close window
    • dealloc (free second alloc'd picker)

A better way might be the way Apple does it in the PhotoLocations and iPhoneCoreDataRecipes examples:

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

Then listen for the didFinishPickingImage and imagePickerControllerDidCancel messages to your delegate and a call to [self dismissModalViewControllerAnimated:YES]; in both places should suffice.

Lavernelaverock answered 19/9, 2009 at 15:28 Comment(2)
I have downloaded sample codes from your given links. even in apple samples memory leaks. please watch the video. It will make the clarification. I am just editing the question nowOdd
I can't stress this enough, if you've found a leak inside the SDK, report the bug so it will get fixedLavernelaverock
S
1

I dont know about the rest of the code, but do you ever have a release?

[imagePickerController release]
Scorbutic answered 19/9, 2009 at 1:9 Comment(4)
Sir - i have surely released that object. The leak is type of ===== +[UIImagePickerController _loadPhotoLibraryIfNecessary]Odd
Well, Apple is not perfect and they sometimes have leaky code. If you give it your best and it is still leaking internally, it is time to focus your efforts on something you can actually change.Scorbutic
also, make sure if you have found a leak in the apple internals, submit a bug through radar so they will fix it. They work the bugs that are reported more first, and the photo library is widely usedLavernelaverock
please watch the video. It will make the clarification. I am just editing the question now.Odd
R
1

UIImagePickerController loads and initializes PhotoLibrary.framework the first time it is shown. This memory won't be reclaimed until your application is closed.

(the code you posted doesn't appear to have leaks as-is, but that doesn't mean it won't interact with the rest of your application in a way that causes them)

Runyan answered 19/9, 2009 at 15:7 Comment(1)
I have downloaded sample codes from your given links. even in apple samples memory leaks. please watch the video. It will make the clarification. I am just editing the question nowOdd
O
1

I can explain this because I was having the same problem.

Don't test memory on the simulator! If you test the apple code on a device the memory problem disappears.

I was having a memory alloc leak which I found in Instruments. All I was doing was opening and closing the image picker (open/cancel) and using Apple code, my code and other people's code, just like yours above.

All were showing the allocation going up and up each time, as if the picker was not being released. If you tried to release it, it would crash (over released).

Then I found a really helpful web page which basically stated:

"This doesn't happen when testing on the device"

So I switched from the simulator and ran the tests on the device. Lo & behold there was no allocation increase and it behaved normally.

This however is totally evil and now we can place no trust in the simulator to do a reliable job.

I want to add this to save people, the time, pain and bewilderment of wondering wtf is going on!

Obtest answered 26/1, 2011 at 5:0 Comment(1)
I still see memory leaks when running it on my phone, just as you say, by opening and canceling. It's pretty horrible. Here's my SO thread:#6554725Improvisatory

© 2022 - 2024 — McMap. All rights reserved.