UIPopovercontroller dealloc reached while popover is still visible

IosMemory ManagementUipopovercontrollerAutomatic Ref-Counting

Ios Problem Overview


I assure you that I did look for an answer in SO for my question but none of them were helpful. Here I got a simple code that should present a UIImagePickerController within a UIPopoverController:

-(void)takePicture:(id)sender{
UIImagePickerController *picker=[[UIImagePickerController alloc] init];
picker.delegate=self;
picker.sourceType=UIImagePickerControllerSourceTypeCamera;
picker.allowsEditing=YES;
UIPopoverController *poc=[[UIPopoverController alloc] 
                            initWithContentViewController:picker];
[poc presentPopoverFromBarButtonItem:bbItem 
            permittedArrowDirections:UIPopoverArrowDirectionAny
                            animated:NO];
}

Now, even from the first time I get [UIPopoveController dealloc] reached while... error and the program crashes. I'm not doing any retain,relase or autoreleases as per ARC. Is there any special consideration with UIPopoverControllers when benefitting from ARC?

Ios Solutions


Solution 1 - Ios

UIPopoverControllers should always be held in an instance variable. It is a good practice to create a strong property for it.

UPDATE:

As of iOS 8 you should be using UIPopoverPresentationController. Then you don't need to keep a reference to the popover because it is managed by the presentation controller.

Code example (works both on iPhone and iPad):

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.allowsEditing = YES;
picker.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController* popoverPC = picker.popoverPresentationController;
popoverPC.barButtonItem = bbItem;
popoverPC.permittedArrowDirections = UIPopoverArrowDirectionAny;
[self presentViewController:picker animated:YES completion:nil];

Solution 2 - Ios

When the function exits there are no other reference to the popover controller, so it's deallocated too early.

Try adding it as a member of your class instead.

Tim

Solution 3 - Ios

Adding what @phix23 answered, create *poc property like this:

@property (nonatomic, retain) IBOutlet UIPopoverController *poc;

and then change

UIPopoverController *poc = [[UIPopoverController alloc] 
                            initWithContentViewController:picker];

for

self.poc = [[UIPopoverController alloc] 
                            initWithContentViewController:picker];

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMikayil AbdullayevView Question on Stackoverflow
Solution 1 - IosFelixView Answer on Stackoverflow
Solution 2 - IostarmesView Answer on Stackoverflow
Solution 3 - IosorafaelreisView Answer on Stackoverflow