views:

3790

answers:

10

Hi there,

I'm trying to display a modal view straight after another view has been presented modally (the second is a loading view that appears).

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    // Show load
    LoadViewController *loader = [[LoadViewController alloc] init];
    [self presentModalViewController: loader animated:NO];
    [loader release];
}

But when I do this I get a "Program received signal: "EXC_BAD_ACCESS"." error.

The stack trace is:

0  0x30b43234 in -[UIWindowController transitionViewDidComplete:fromView:toView:]
1  0x3095828e in -[UITransitionView notifyDidCompleteTransition:]
2  0x3091af0d in -[UIViewAnimationState sendDelegateAnimationDidStop:finished:]
3  0x3091ad7c in -[UIViewAnimationState animationDidStop:finished:]
4  0x0051e331 in run_animation_callbacks
5  0x0051e109 in CA::timer_callback
6  0x302454a0 in CFRunLoopRunSpecific
7  0x30244628 in CFRunLoopRunInMode
8  0x32044c31 in GSEventRunModal
9  0x32044cf6 in GSEventRun
10 0x309021ee in UIApplicationMain
11 0x00002154 in main at main.m:14

Any ideas? I'm totally stumped! The loading view is empty so there's definitely nothing going on in there that's causing the error. Is it something to do with launching 2 views modally in the same event loop or something?

Thanks,

Mike

Edit: Very strange... I have modified it slightly so that the loading view is shown after a tiny delay, and this works fine! So it appears to be something within the same event loop!

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    // Show load
    [self performSelector:@selector(doit) withObject:nil afterDelay:0.1];
}

- (void)doit {
    [self presentModalViewController:loader animated:YES];  
}
A: 

EXC_BAD_ACCESS is a memory error. You're probably trying to use an object that has already been released/deallocated. This answer gives some tips for debugging these issues:

Debugging EXC_BAD_ACCESS

pix0r
I have the exact same problem in a minimal test app, and I know it's not normal memory management issue.
Marvin
A: 

It really depends on what the support routines for viewDidAppear are doing. For example, if presentModalViewController:animated: does not retain loader the crash may be due to the UIWindowController trying to speak about loader which has since been released (at the end of the routine you posted).

fbrereto
Thanks for your comment. I've tried storing it in an instance variable so it is definitely retained and I'm still getting the error.
Michael Waterfall
There are two views in play here -- the one coming in and the one going out -- are you retaining both until the transition is complete? Maybe there's an issue with keeping both views alive until then.
fbrereto
+1  A: 

Your problem is most likely in the method that inits and presents the method that viewDidAppear is in, or in the init/viewDidLoad/viewWillAppear method of LoadViewController.

Set some break points and follow until crash...

Jordan
Those methods are pretty much empty. All I do is change background colour on viewDidLoad:. The application executes these lines fine, the crash doesn't occur in my code, it's crashing when other things are being performed later on in the event loop.
Michael Waterfall
+5  A: 

I have modified it slightly so that the loading view is shown after a tiny delay, and this works fine! So it appears to be something within the same event loop!

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    // Show load
    [self performSelector:@selector(doit) withObject:nil afterDelay:0.1];
}

- (void)doit {
    [self presentModalViewController:loader animated:YES];  
}
Michael Waterfall
Man, I've been trying to figure this out for a while now. Thanks for the help!
Justin Gallagher
Thank you, thank you, thank you.This has been killing me.
Danilo Campos
I suspect it'll work with a delay of 0.0f as well, then. Basically, it will be presented in the next iteration of the runloop, essentially instantly.
Thomas Müller
+1  A: 

I think this issue has something to do with a problem that I also encountered. It is very easy to reproduce:

Create new XCode Project "Utility Application". In the FlipsideViewController.m you just insert the following method:

  • (void)viewDidAppear:(BOOL)animated { [super viewDidAppear: animated]; [self showInfo]; }

If you do this, start the application, then the flipside-view will be activated right away. As soon as you press the "Done" Button on the flipside-view, you will get back to the Mainview which fires viewDidAppear again and goes right back to the flipside- view. As soon as the flipside-view is displayed, the application stops - no memory deallocators are called - it is just like you have pressed the home-button.

When I was using some additional properties in those views, I also got the exception, so I stripped down the code to the minimum amount...

I really have no clue, what this problem really is...

Best regards, Tobias

Tobias
A: 

I had a similar problem while using the same technique as you to implement a loading view. It would crash when the loading view was dismissed at the end of the load. In my case, the problem came from the fact that as soon as the loading view was dismissed viewDidAppear was called again and tried to present the loading view again, which presumably triggered the crash. I fixed it simply by checking if the loading view had been presented before:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    if(needDisplayLoader)
        [self presentModalViewController: loader animated:NO];
}

Then I set needDisplayLoader to NO before dismissing the Loader view

Hope this helps...

Rom
A: 

I ran into this issue just now, and corrected it using the selector:afterDelay suggestion above. Just to add, I compiled (without the fix) under iPhone OS 4.0 beta, and NO CRASH! So, the bug in XCode appears to have been fixed in the next gen. Not that this does any of us any good today, but, just so you all know, it truly was a bug in Xcode and not necessarily anything we were doing wrong in our coding styles.

pulseft
A: 

I believe I reproduced the same error in iOS 4. In my application, the crash occurred consistently when attempting to show a second modal view immediately after showing a first modal view. I struggled for a few hours going insane.

After reading the posts in this thread, I tried to create a simple reproducible example using the Tab Bar Application template. I was able to use the UIImagePickerController to show the first modal view after responding to a button click in "FirstViewController.m". When I tried to show the UIImagePickerController again (after handling the imagePickerControllerDidCancel message), the application crashed with the same error.

On the device, there was simply no clue what was going on. However, when I ran the code on the Simulator, I was lucky enough to get this message on the console:

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempting to begin a modal transition from to while a transition is already in progress. Wait for viewDidAppear/viewDidDisappear to know the current transition has completed'

So it seems that my only choice is to follow the advice of the error message and simply wait until viewDidAppear (using a flag to indicate I'm in this special mode) and then load the second modal view.

Here's the full stack trace for completeness:

** Call stack at first throw: ( 0 CoreFoundation 0x0238c919 exceptionPreprocess + 185 1 libobjc.A.dylib 0x024da5de objc_exception_throw + 47 2 CoreFoundation 0x02345078 +[NSException raise:format:arguments:] + 136 3 Foundation 0x000ab8cf -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116 4 UIKit 0x00544317 -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:] + 212 5 UIKit 0x0035c769 -[UIViewController presentModalViewController:withTransition:] + 2937 6 TestTempDelete 0x000021cf -[FirstViewController showImagePicker] + 167 7 Foundation 0x0002fcea __NSFireDelayedPerform + 441 8 CoreFoundation 0x0236dd43 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION + 19 9 CoreFoundation 0x0236f384 __CFRunLoopDoTimer + 1364 10 CoreFoundation 0x022cbd09 __CFRunLoopRun + 1817 11 CoreFoundation 0x022cb280 CFRunLoopRunSpecific + 208 12 CoreFoundation 0x022cb1a1 CFRunLoopRunInMode + 97 13 GraphicsServices 0x02bf12c8 GSEventRunModal + 217 14 GraphicsServices 0x02bf138d GSEventRun + 115 15 UIKit 0x002beb58 UIApplicationMain + 1160 16 TestTempDelete 0x00001eb4 main + 102 17 TestTempDelete 0x00001e45 start + 53

Hope this helps.

Daniel
A: 

Had exact same problem. Solved it with the above suggested...

[self performSelector:@selector(doit) withObject:nil afterDelay:0.5];

Had to use a 0.5 sec delay. Possibly because I was performing presentModalViewController directly after a UIPickerViewController modal.

Nate Potter
A: 

I just had this problem and it turned out that my problem was because I was dealloc my protocol delegate.

evanchri