mercredi 28 janvier 2015

iPhone app crashes when executing block code, without any console output

Here is my button action method on ViewController_1. After I click it, it will bring up a new custom view CHMenuView.



- (IBOutlet)centerButtonAction:(id)sender
{
CHMenuView *menuView = [[CHMenuView alloc] init];

[menuView addMenuItemWithTitle:@"take picture"
andIcon:[UIImage imageNamed:@"camera_ps.png"]
andSelectedBlock:^{
NSLog(@"take picture");

// If I comment out all the rest code below in the block
// the app won't crash, and it will print "take picture"
// in the console
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
}
else
{
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
[imagePicker setDelegate:self];
[self presentViewController:imagePicker animated:YES completion:nil];
}];
[menuView show];
}


CHMenuView is a custom view, and it has CHMenuItemButton on it. If I clicked the CHMenuItemButton, the block code defined in "andSelectedBlock" will get executed, and the app crashes.


This is the instruction pointer points to when the app crashes:



- (void)buttonTapped:(CHMenuItemButton*)btn
{
[self dismiss:nil];
double delayInSeconds = CHMenuViewAnimationTime + CHMenuViewAnimationInterval * (buttons_.count + 1);
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
btn.selectedBlock(); // Thread 1: EXC_BAD_ACCESS(code=1, address=0x600)
});
}


No console output. If I comment out all the UIImagePicker stuff in the block code, NSLog() will print the string, and app won't crash. But the block code seems harmless to me, I don't know what I had done wrong.




Aucun commentaire:

Enregistrer un commentaire