views:

214

answers:

2

If the Help option is activated in my app when the user goes to the camera I show a UIAlertView first with tips on how to take a picture:

-(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex != [actionSheet cancelButtonIndex]) {
    NSString *selectedButtonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
    if ([selectedButtonTitle isEqualToString:@"Camera"]) {
        // If Help is activated display camera tips
        if (helpEnabled == YES) {
         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Camera Tips" message:@"\n\n\n\n\n\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Go To Camera"];
         UIImageView *cameraHelpView = [[UIImageView alloc] initWithFrame:CGRectMake(17, 40, 250, 255)];
         UIImage *cameraTutorial = [UIImage imageNamed:@"Camera_Tips.png"];
         cameraHelpView.image = cameraTutorial;
         [alert addSubview:cameraHelpView];
                    [cameraHelpView release];    
         [alert show];
         [alert release];
        }
    }
}

This works in Debug mode but causes an "EXC BAD ACCESS" error in Release mode. I can present a new view controller modally from this point just fine, however the UIAlertView will always crash the app. Why?

A: 

I don't know why it works in debug mode, but it looks like you are releasing your cameraHelpView while it is still being used. In the subviews of alert is a pointer to cameraHelpView; when you release that, it can no longer be accessed. I would suggest replacing all of your -[NSObject release] calls in this context with -[NSObject autorelease]. Thus:

UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Camera Tips" message:@"\n\n\n\n\n\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Go To Camera"] autorelease];
UIImage *cameraTutorial = [UIImage imageNamed:@"Camera_Tips.png"];
UIImageView *cameraHelpView = [[[UIImageView alloc] initWithFrame:CGRectMake(17, 40, 250, 255)] autorelease];
cameraHelpView.image = cameraTutorial;
[alert addSubview:cameraHelpView];
[alert show];

Go ahead and try that and let me know if it works. Good luck!

Jonathan Sterling
Thanks for the response. When cameraHelpView is added as a subview to the UIAlertView the alert will retain a copy of cameraHelpView until the alert is released. That is why it is safe to release cameraHelpView after adding it as a subview.
Ian
Oops! Sorry about the misinformation.
Jonathan Sterling
A: 

I found my mistake. I wasn't passing nil as the final argument to otherButtonTitles! Debug mode must see and fix this error for you. Hope this helps someone.

Ian