2010-10-27 72 views
0

我收到以下代码的错误消息。本质上,应用程序确认在从表格视图中选择时调用该号码。当具有此警报的ViewContoller被取消时,EXC_BAD_ACCESS即将进入。UIAlertView正在导致EXC_BAD_ACCESS

只有在警报被触发时才会发生。如果仅在没有选择的情况下查看表格,则不会发生。这告诉我,我正在做这个UIAlertView错误。可能与内存管理有关并发布我不应该做的事情。

我哪里错了?

phoneAlert = [[[UIAlertView alloc] initWithTitle:locationName message:displayNumber delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Call",nil] autorelease]; 
    [phoneAlert show]; 
[tableView deselectRowAtIndexPath:indexPath animated:YES]; 


- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 1) { 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",dialNumber]]];  
    } 

} 

- (void)dealloc { 
    [locations release]; 
    [location release]; 
    [dialNumber release]; 
    [phoneAlert release]; 
     [super dealloc]; 
} 
+0

重复的问题:http://support.microsoft.com/kb/4212580/uialertview-exits-exc-bad-access-error/20058822#20058822 – 2013-11-18 22:06:06

回答

3

您分配phoneAlert自动释放UIAlertView,这是不,因为你不使用点语法或setter方法由您的实例保留,你在做直分配。

所以,如果你定义phoneAlert为与retain关键字的属性,那么你应该这样做是为了得到期望的结果:

self.phoneAlert = ... 

[self setPhoneAlert:...]; 

否则,你会得到EXC_BAD_ACCESS您的dealloc方法,因为您autorelease d警报视图,因此该实例被autorelease池释放。打开项目中的僵尸,看看这个行动。

+0

对,感谢您的帮助。我真的回去了,意识到我正在让它变得比它真正需要的复杂。特别是只是一个确认消息。我应该更频繁地这样做。 – TheHockeyGeek 2010-10-27 15:55:18