线程问题(EXC_BAD_ACCESS)

时间:2010-03-29 15:07:10

标签: iphone memory crash multithreading

以下代码崩溃了应用程序,我不知道为什么。它不规则地崩溃,这意味着有时单击时可以显示图像视图30次,有时候当我选择一行时它会第二次崩溃。

仅供参考:活动指标,操作表和变量imageNr是全局定义的,将在viedDidLoad方法中初始化。

提前感谢您的建议。
肖恩

- (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];
}

1 个答案:

答案 0 :(得分:1)

除此之外,您还在后台线程上执行用户界面更新(通过在后台运行-showImageView)。用户界面元素在iPhone OS上不是线程安全的,必须在主线程上更新。实际上,-showImageView方法中所做的一切都需要在主线程上完成(-setViewInfo可能除外,具体取决于它的作用)。

另外,在后台线程上释放对象时要非常小心,因为它们可能在主线程(或其他线程)上使用,这可能会导致崩溃。