Question

Okay, so I have just started iOS development. I'll start by explaining the flow of my app :
1. A view called "appViewController" is loaded.
2. [self presentViewController: controller animated: YES completion:nil]; this loads a webview
3. After I am done with the webview, I dismiss it and load a new UINavigation this way :

[self dismissViewControllerAnimated:YES completion:^{
        formViewController *fv = [ [formViewController alloc] init ];
        UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:fv] autorelease];
        navController.navigationBar.hidden = YES;
        [presentingViewController presentModalViewController:navController animated:YES];

    }];

5.The formViewController has a button, which has the event attached to it for that display an alert this way

UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Oops!"
                                           message:@"test"
                                          delegate:nil
                                 cancelButtonTitle:@"OK"
                                 otherButtonTitles:nil];
[av show];

Everything works as intended up till here. Now when I click the "Ok"(Cancel) button, the app crashes with NSZombieEnabled saying
-[appViewController _deepestDefaultFirstResponder]: message sent to deallocated instance 0x6e6a570 lldb

What is happening here? Why is it trying to send message to appViewController again? There is no code after [av show]

NOTE : I am using ARC

Was it helpful?

Solution

If you're using arc, the autorelease in your code isn't valid.

This seems like your root view controller gets deallocated at some point and when the responder chain gets traversed the resulting dangling pointer gets accessed.

To verify this, I would implement the dealloc method on appViewController and see if it gets called.

dealloc {
  NSLog(@"Problems ahead.");
}

If this does get called before you'd expect that to happen (for a root view controller probably not at all), you need to find out why this happens. You're probably missing a strong reference somewhere. Check you app delegate and verify that you have a strong reference to the window and that you are setting your controller as the root view controller (provided you're not using storyboards).

The Zombies instrument is very good for debugging such problems. It will list all retains and releases of your problematic object. Here's a short introduction to it.

OTHER TIPS

A couple of things.

  1. You are mixing presentViewController and presentModalViewController. If you are new to iOS, you should always use presentViewController. No need getting used to using a method that will soon be deprecated (see answer to this question Difference between presentModalViewController and presentViewController?

  2. In general, you shouldn't autorelease unless you absolutely have to. Since when a controller is presented with presentViewController (and presentModalViewController), it is retained, you can readily release afterwards. I would restructure like so:

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:fv];
navController.navigationBar.hidden = YES;
[presentingViewController presentModalViewController:navController animated:YES];
[navController release];

3.. Where is the section of code you included located? Like, you push a button and the view is dismissed or what?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top