UIImagePickerController does not release memory it

2019-02-13 09:35发布

问题:

I saw this thing on using instruments for my app. When I profile my app, the initial memory occupied is 563 KB which is before UIImagePickerController pops up. There is one button on the first viewController which makes the UIImagePickerController appear.
As soon as UIImagePickerController appears, memory occupied goes upto 1.6 - 1.7 MB. If I select any image or cancel the UIImagePickerController, the memory occupied is still 1.6 - 1.7 MB which I believe should be 563 KB(or may be few KB's more).
Please see the below code I have used :

- (IBAction)chooseButtonPressed:(id)sender
{
    UIImagePickerController *pickerController = [[UIImagePickerController new]autorelease];
    [pickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    [pickerController setDelegate:self];
}  

Why is the memory not being released?

回答1:

We can't add images to comments so i'm putting this as an answer. The Live Bytes are always less than the Overall Bytes except till the first time a memory is deallocated. This can be seen from the image below.

I don't think there's anything wrong with your deallocation. I think you're just looking at the wrong values!

EDIT- I Think the problem might be somewhere else. To see the values I was seeing, you need to make a little change. As shown in the image below, you need to uncheck the option Only track active allocations to see the values you're looking for. If you still see 7.41 MB in Active allocations, then the problem is something else.



回答2:

Since you have given it autorelease option it will get added to the autorelease pool ... see what the documentation say..

The Application Kit creates an autorelease pool on the main thread at the beginning of every cycle of the event loop, and drains it at the end, thereby releasing any autoreleased objects generated while processing an event.

you can always release the picker in the delegate call like this..

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
...
...
[picker release];

}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
....
....
[picker release];

}


回答3:

try this

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


回答4:

Did you set delegate to nil?

For more information you can refer to UIImagePickerConrtoller class reference

[picker release];
picker.delegate = nil ;

Hope this helps you.