views:

400

answers:

3

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

+1  A: 

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

[imagePickerController release]
coneybeare
Sir - i have surely released that object. The leak is type of ===== +[UIImagePickerController _loadPhotoLibraryIfNecessary]
sugar
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.
coneybeare
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 used
slf
http://developer.apple.com/bugreporter/
slf
please watch the video. It will make the clarification. I am just editing the question now.
sugar
+1  A: 

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)

rpetrich
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 now
sugar
+2  A: 

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.

slf
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 now
sugar
I can't stress this enough, if you've found a leak inside the SDK, report the bug so it will get fixed
slf