问题
I'm opening a camera for the user to take a picture. I keep getting a memory leak when I took a picture and pressed "use" on: [self presentModalViewController:imagePicker animated:YES],
Full code:
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = [NSArray arrayWithObjects:
(NSString *) kUTTypeImage,
(NSString *) kUTTypeMovie, nil];
imagePicker.allowsEditing = NO;
[self presentModalViewController:imagePicker animated:YES]; //This leaks
In both didFinishPickingMediaWithInfo
and imagePickerControllerDidCancel
I put this line:
[imagePicker dismissModalViewControllerAnimated:YES];
I do know this question has been asked before but none of them seen to help me any further with the leak I have got.
回答1:
If it's not an ARC env:
Your
imagePicker = [[UIImagePickerController alloc] init];
returns retain count +1,
then
[self presentModalViewController:imagePicker animated:YES]
retains your controller, so retain count +2,
on [imagePicker dismissModalViewControllerAnimated:YES];
it's +1, so you still have your controller hanging in memory.
Release your controller after presentModalViewController
.
回答2:
Try this code
imagePicker = [[[UIImagePickerController alloc] init] autorelease];
And get sure what you have
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
// your code
[pool release];
回答3:
Turns out this is a bug in the code of iOS itself.
I downloaded the sample code of the Apple Developer Website and the same leak turned up. So this will be nothing I can fix myself and I hope this gets corrected soon.
回答4:
How about creating a @property
for imagePicker
and assigning:
self.imagePicker = [[UIImagePickerController alloc] init];
来源:https://stackoverflow.com/questions/10752917/memory-leak-on-presentmodalviewcontroller