开发者

threading problem (EXC_BAD_ACCESS)

the following code crashes the application, and I don't know why. It crashes irregularly, that means that sometimes the imageview can be shown for example 30 times when a row is clicked, sometimes it chrashes the second time when I select a row.

FYI: the activity indicator,the action sheet and the variable imageNr are defined globally and will be initialized in the viedDidLoad method.

Thanks in advance for your advices.

Sean

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 imageNr = indexPath.row + 1;
 activityLoadView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(-17.5, -11, 35, 35)];
                    activityLoadView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
                    activityLoadView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
                                                        UIViewAutoresizingFlexibleRightMargin |
                                                        UIViewAutoresizingFlexibleTopMargin |
                                                        UIViewAutoresizingFlexibleBottomMargin);

 actionSheetLoadView = [[UIActionSheet alloc] initWithTitle:@""
                                                   delegate:self
                                          cancelButtonTitle:nil
                                     destructiveButtonTitle:nil
                                          otherButtonTitles:nil];
 actionSheetLoadView.actionSheetStyle = UIBarStyleBlackOpaque;

 [actionSheetLoadView setMessage:[NSString stringWithFormat:@"\n\n\n\n\n\n\n\n\n\n\n\n\nLoading image ..."]];
 [actionSheetLoadView addSubview:activityLoadView];
 [actionSheetLoadView showInView:self.view];
 [actionSheetLoadView setBounds:CGRectMake(0,0,320,720)];

 开发者_运维问答[activityLoadView performSelector:@selector(startAnimating) withObject:nil afterDelay:0.1];
 [self performSelectorInBackground:@selector(showImageView) withObject:nil];
}

- (void)showImageView
{
  [self setViewInfo];
  [self.navigationController pushViewController:imageViewController animated:YES];

  [activityLoadView performSelector:@selector(stopAnimating) withObject:nil afterDelay:0.1];
  [actionSheetLoadView dismissWithClickedButtonIndex:0 animated:YES];
  [activityLoadView release];
  [actionSheetLoadView release];
}


Among other things, you are performing user interface updates on a background thread (by running -showImageView in the background). User interface elements are not threadsafe on the iPhone OS, and must be updated on the main thread. Practically everything done in the -showImageView method needs to be done on the main thread (with the possible exception of -setViewInfo, depending on what that does).

Also, be very careful when releasing objects on a background thread, as they may be in use on the main thread (or another), and this may lead to crashes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜