uiimagepickerview controller creating memory leaks

2020-03-07 10:02发布

问题:

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:

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.



回答2:

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

[imagePickerController release]


回答3:

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)



回答4:

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!