0

I have a viewcontroller called via UIStoryboardPopoverSegue and inside there is a button to call the imagePickerController, it's ok for the first call but the second time it crash.

The code is use is ok when it is not through UIStoryboardPopoverSegue.

   -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
    UIImageWriteToSavedPhotosAlbum (image, nil, nil , nil);
    [self dismissViewControllerAnimated:YES completion:nil];

}
-(IBAction)takephoto:(id)sender
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        self.imagePicker.delegate=self;
        self.imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera;

        [self presentViewController:imagePicker animated:YES completion:nil];
    }

}

- (void)viewDidLoad
{
    [super viewDidLoad];

    if (!self.imagePicker)
    {
        self.imagePicker = [[UIImagePickerController alloc] init];
    }
}

I enabled NSZobmbie to show any log that can help me tracing it.

* -[UIImagePickerController isKindOfClass:]: message sent to deallocated instance 0x1eb3b700

baste
  • 827
  • 2
  • 9
  • 18

1 Answers1

1

Your UIImagePickerController is released and then attempted to be accessed later - so crashes!

To fix it, make the UIImagePickerController a strongly referenced property of the owning instance:

@property (strong, nonatomic) UIImagePickerController *imagePicker;

initialise it once:

if (!self.imagePicker) self.imagePicker = [[UIImagePickerController alloc] init];

and use:

[self.imagePicker doStuff]

for access.

That should solve it.

EDIT

if ([[segue identifier]isEqualToString:@"tcwindshield"]) { 
   self.ips = [segue destinationViewController]; 
   self.ips.delegate = self
   self.ips.strStatValue=@"WindShield"; 
}
Adam Waite
  • 19,175
  • 22
  • 126
  • 148
  • thank you @Adam for the reply, apology if I'm confused with your instruction please check my revised code because it still crashes. ` if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { if (!self.imagePicker) self.imagePicker = [[UIImagePickerController alloc] init]; [self.imagePicker delegate]; self.imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera; [self presentViewController:self.imagePicker animated:YES completion:nil]; } ` – baste Jul 26 '13 at 16:19
  • self.imagePicker.delegate = self; – Adam Waite Jul 26 '13 at 16:31
  • I'm still getting the crash issue *** -[UIImagePickerController isKindOfClass:]: message sent to deallocated instance 0x1fd317c0 :( – baste Jul 26 '13 at 18:54
  • thanks Adam for your time for this i really need help and the bug is very strange. I updated the code now, what i understood with initialize it once is i placed if `(!self.imagePicker) self.imagePicker = [[UIImagePickerController alloc] init];` under viewDidLoad. – baste Jul 28 '13 at 21:41
  • Have you added an exception breakpoint? That might show you were the program crashes. http://blog.manbolo.com/2012/01/23/xcode-tips-1-break-on-exceptions. – Adam Waite Jul 29 '13 at 06:32
  • Is the viewcontroller called via UIStoryboardPopoverSegue a strong property? Maybe that's released? – Adam Waite Jul 29 '13 at 06:34
  • I added the exception breakpoint [link](https://docs.google.com/file/d/0ByavDvkVnkwgYVQwbkVJWHF5SUE/edit?usp=sharing) '@property (nonatomic, strong)InspectionPopStatViewController *ips;' 'if ([[segue identifier]isEqualToString:@"tcwindshield"]) { ipspop=(UIStoryboardPopoverSegue *)segue; ips=[segue destinationViewController]; [ips setDelegates:self]; ips.strStatValue=@"WindShield";' }' – baste Jul 29 '13 at 10:16
  • you need to do self.ipspop when you want to use your property. That's your issue. – Adam Waite Jul 29 '13 at 12:52
  • i'm still getting the crash and its the same error message.:( `if ([[segue identifier]isEqualToString:@"tcwindshield"]) { ipspop=(UIStoryboardPopoverSegue *)segue; self.ips=[segue destinationViewController]; self.ips.delegates=self; self.ips.strStatValue=@"WindShield"; }` – baste Jul 29 '13 at 13:29