In the following code I alloc a UIPopoverController. Obviously I am not releasing it here because I am not sure where I should properly release it without a crash.
Where should I release it?
- (IBAction)photoLibraryiPad {
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
self.popoverController = [[UIPopoverController alloc]
initWithContentViewController:imagePicker];
popoverController.delegate = self;
[self.popoverController presentPopoverFromRect:myButton.frame inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[imagePicker release];
}
}
Thanks!
You should keep a reference to it in your view controller and release it when you’re done.
But in that case, you’re both setting the property and creating a already-retained object.
Here,
[[UIPopoverController alloc]initWithContentViewController:imagePicker];creates a retained object that you must release.But you’re setting the property
popoverControllerto that value, and the setter of that property probably retain that value.You’re retaining the popover twice and never releasing it.
You should :
And when you’re done with the popover :
You should probably review your memory management rules to make that clear or use
ARC.