views:

73

answers:

3

Hi,

I have an iPad application that builds and runs perfectly the first time. But if I exit the application and open it again, the interface is completely unresponsive. If I exit and open another time, it never gets past the splash screen.

What's strange is that if I wait a minute or two before opening it again, it always runs fine.

Any ideas on what might be going on or where I should start my debugging efforts? I would throw in breakpoints and see what's going on, but by the time I start the application a second time, the debugger has already exited. Is there a way to keep the debugger and console running through multiple executions of an app?

Thanks,

Luke

Edit: Here's some code I use for NSUserDefaults - could this be the problem?

In viewDidLoad in my main view controller:

bgnum = [prefs integerForKey:@"bgnum"];
menuVisible = [prefs boolForKey:@"menuVisible"];
songTitles = [[NSMutableArray alloc] initWithArray:[prefs arrayForKey:@"songTitles"]];
numberOfSongs = [prefs integerForKey:@"numberOfSongs"];

In viewWillDisappear:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

[prefs setBool:menuVisible forKey:@"menuVisible"];
[prefs setInteger:bgnum forKey:@"bgnum"];
[prefs setObject:songTitles forKey:@"songTitles"];
[prefs setInteger:numberOfSongs forKey:@"numberOfSongs"];

[prefs synchronize];
A: 

With the device connected, run the app in debug mode from xCode and then exit the app. Now, disconnect the USB cable and run your app again - is it completely frozen? If you rotate the device, does your view orientation change? Once the app is terminated, plug your USB back in and go to xCode->Window->Organizer.

You should see your iPad device on the left of the screen. Select it and you should see some tabs on the right for Console and Crash report. Select your app from the dropdown and look to see if any console messages have been logged, or if a crash has occured - you should get a stack trace if it did which should help.

I once had a similar problem when I had NSZombieEnabled as an active argument in my executable, so that could be worth investigating too.

davbryn
Still freezes, with no orientation change. There are no crash reports, and NSZombieEnabled is not there. Not sure what's up - I'll keep looking around.
Luke
+1  A: 

I'd look at what's happening in your initialization code, loadView, didFinishLaunching, etc., and trace it out. It's easy to get 3 or 4 methods deep in that stuff and do too much there when some of it should be lazy. With out more information or any sample code it's all wild guesses. One such guess is user defaults or anything that you're loading from a file or dictionary up front? I could see how something there could cycle through several states.

badweasel
That's a great point - I think NSUserDefaults is a likely culprit. Sorry I haven't provided code - it's difficult to know where to begin. I've added the code I use for NSUserDefaults above - any thoughts on what could be wrong?
Luke
A: 

There's a big difference between "exiting the application and opening it again" which in context implies you are doing it rather quickly and "waiting a minute or two".

I have a rather large application that I develop on which routinely takes several seconds to "unload" on the iPad, which results in me rapidly exiting and re-entering causing the application to appear in a partially configured state, and causing unusual behavior.

This could be due to the fact that your application hasn't completely dealloc'd and closed its threads out, and thus when you launch it again quickly, the thread is resumed (and on 3.2 or earlier, will be unstable since it's already started killing itself off).

Do some timings... see what the threshold is for "it crashes" and "it works". If it's more than 10 seconds, then I would say something is wrong. Less than that, and you could be seeing what I just described,

Jasconius
That's probably true - have you found a good way to avoid the problem in case the user exits and re-enters very quickly?Waiting about five seconds seems to make it work every time. Waiting only three seconds causes a crash.
Luke
Well. One way is you can take solace in the fact that in iOS4, by default your application does not attempt to dealloc itself completely. Even complicated apps (such as mine) will hold station quite well and without incident... so in iOS4 what you are trying to do would work naturally. In 3.2 or 3.1? Not much. You can make sure that you are doing as much object pruning as possible whenever appropriate so that when the application completely exits it has less to get rid of.
Jasconius