views:

36

answers:

1

I get an error with the following code. Essentially the app confirms calling the number when it is selected from the table view. The EXC_BAD_ACCESS is coming in when the ViewContoller with this alert is dismissed.

It only happens if the alert is triggered. It does not if the table is only viewed with no selection. That tells me I am doing something wrong with this UIAlertView. Probably having to do with memory management and releasing something I should not be.

Where am I going wrong?

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];
}
+2  A: 

You are assigning phoneAlert to an autoreleased UIAlertView, which isn't being retained by your instance because you're not using dot syntax or the setter methods, you're doing straight assignment.

So, if you defined phoneAlert as a property with the retain keyword, then you should do this to get the desired result:

self.phoneAlert = ...

or

[self setPhoneAlert:...];

Otherwise you will get EXC_BAD_ACCESS in your dealloc method, because you autoreleased the alert view, so the instance was deallocated by the autorelease pool. Turn on zombies in your project to see this in action.

Jacob Relkin
Right on, thanks for the assistance on this. I actually went back and realized I was making it way more complex than it really needed to be. Especially for just a confirmation message. I should be doing that more often.
TheHockeyGeek
@TheHockeyGeek Can you upvote this as well? Thanks!
Jacob Relkin