I’ve been trying to debug this issue for hours upon hours but with no luck. I have a button that when pressed simply does this:
[self.parentViewController dismissModalViewControllerAnimated:NO];
Now I have a lot of AVAudioPlayers and AVAudioRecorders going on, but I’m sure to handle all that carefully before exiting. The weird thing is that pressing this button doesn’t always cause the app to crash. Only after a certain amount of time has passed, the app crashes upon clicking. So if I press the button 2 seconds after the page loads, then I am able to dismiss the view with no problems, and it goes back to other view. However if I wait 9 or more seconds, I get a crash.
I know it’s impossible to help me with this little info, but how do I begin debugging this issue? I don’t get any useful output when it crashes, just BAD_ACCES and no message at all. How can I look deeper into this and find what’s going on? The debugger isn’t helping either.
EDIT: I’m not sure if I’ve fixed the issue since it’s random, but when I first create the view controller that I will later dismiss, I do this:
CloseDoorViewController *closeVC=[[CloseDoorViewController alloc] init];
[self.view addSubview:closeVC.view];
[self presentModalViewController:closeVC animated:NO];
[closeVC release];
Then when I’m in CloseDorView, and I hit dismissModalViewController, I get a crash. But after commenting out [closeVC release];, the issue goes away (I think). So am I not supposed to be releasing closeVC? What is the proper way to do this?
What I suspect is happening here is that you’re trying to dismiss the modal view controller from within a button click handler in the actual modal view controller’s code. Here is what I always do when displaying a modal view controller:
In this example, vc1 is the “parent” (I use that loosely) view controller that will present vc2.
(1). create a protocol (“ModalViewControllerDelegate.h”):
(2). edit vc1 like so:
(3). edit vc2 like so:
(4). present vc2 from vc1 like so:
Explanation:
vc1 creates vc2 and sets itself as the delegate for vc2… when the dismiss button is clicked in the view of vc2, it checks for the existence of a delegate, finds vc1, and the appropriate selector/method in vc1 fires…which dismisses vc2.
I hope that helps.