简体   繁体   中英

UIAlertView causes crash in Release mode

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?

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM