4

Okay, so from fresh (app not previously installed on the iPhone simulator), the app boots up fine. Then I press the home button and click on the icon and it also is fine. Then if I press the home button, then close the app from the multitasking bar, then press the icon I get the SIGKILL error.

However, when I press run in Xcode it ALWAYS works flawlessly without fail (even after I have close it from the multitasking bar, where pressing the icon fails). Is this just a quirk with the simulator? This behaviour has only begun after I have implemented some NSUserDefault stuff, to remember it's state etc. It does remember all of the defaults though, when it works.

Any help is appreciated.

EDIT:

- (void)viewDidLoad
{

NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
                                 [NSNumber numberWithInt:1], @"firstRun",
                        nil];
[defaults registerDefaults:appDefaults];
if ([[defaults objectForKey:@"firstRun"] intValue] == 1) {
//do the stuff required at first launch
table = [NSMutableArray array];
Stocks =[NSMutableArray array];
Money =1234.56;
mem=@"GOOG";
[defaults setDouble:Money forKey:@"money"];
[defaults setObject:mem forKey:@"ticker"];
[defaults synchronize];
self.Input.text=mem;


- (void)viewWillAppear:(BOOL)animated
{

NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
if ([[defaults objectForKey:@"firstRun"] intValue] == 0) {
    [self entered:nil];

} else if ([[defaults objectForKey:@"firstRun"] intValue]== 1){
    [defaults setObject:[NSNumber numberWithInt:0] forKey:@"firstRun"];
    [defaults synchronize];
}
[super viewWillAppear:animated];
}

Here is what I think may be relavent regarding NSUserDefaults in my MainViewController (I don't use UserDefaults in any other viewController).

I also do a few setObjects/synchronizes in a few other methods, but they only execute when a button is clicked (which doesn't happen when it crashes).

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Greg Cawthorne
  • 396
  • 5
  • 21
  • I did check that. All it said was: Attaching to process 1196. [Switching to process 1196 thread 0x11903] – Greg Cawthorne Aug 31 '12 at 20:36
  • Strange, indeed. +1 in the meantime. –  Aug 31 '12 at 20:37
  • Like I said, it only happens when launching up after the first time. But it works fine if you press run in Xcode, instead of pressing the icon (when it would normally crash). It also loads all of the User Defaults that were set on the first launch, so I am reluctant to think that it is the way I have used NSUserDefaults (because if it was I doubt it would boot when launched from Xcode). I could always upload my project if that would be helpful. – Greg Cawthorne Aug 31 '12 at 20:42
  • I think you read a settings from NSUserDefaults that's not there for the first time, then it returns nil and some Foundation class decides to crash upon being passed nil. –  Aug 31 '12 at 20:44
  • Added my use of NSUserDefaults to the OP. – Greg Cawthorne Aug 31 '12 at 20:53
  • thanks. Nothing obvious problem there... –  Aug 31 '12 at 20:58
  • After you close your app in the multitasking bar, before tapping the icon, did you stop your run in Xcode? – thvanarkel Aug 31 '12 at 21:00
  • Ahhhhh.... Sneaky sneaky. No I wasn't pressing stop in Xcode. When I do it works! Does this mean I can carry on as normal and when it's actually on an iPhone, closing it from the multitasking bar will do the same job as pressing stop in Xcode? Or is there some mysterious code I can use to programmatically solve this problem? – Greg Cawthorne Aug 31 '12 at 21:07
  • @GregCawthorne: No, closing from the multi-tasking bar won't do the same things as pressing stop from xCode. Your are abruptly killing your processes by closing from the multitasking bar. – mvb Aug 31 '12 at 21:10
  • @GregCawthorne No, you should first stop in Xcode and then if you wish you can close the app on the device/simulator. – thvanarkel Aug 31 '12 at 21:13
  • How can i solve this, seeing as this could be a realistic possibility in real world settings? – Greg Cawthorne Aug 31 '12 at 21:13
  • 1
    @GregCawthorne: This won't happen once your app is installed on a device and not connected to Xcode for running/profiling/testing. – mvb Aug 31 '12 at 21:14

1 Answers1

3

When you remove your the application from your multitasking bar, you are actually permanently closing it. Hence you destroy the instance (all processes get killed), which the XCode associated with your app. This is why you get the SIGKILL error.

More on the iPhone Simulator: http://developer.apple.com/library/ios/#DOCUMENTATION/Xcode/Conceptual/ios_development_workflow/25-Using_iOS_Simulator/ios_simulator_application.html

In fact this would even happen if you ran your application on an actual device, and deleted it from the multitasking bar, while the application was connected to XCode. However if your application is installed on a device and not connected to Xcode for running/testing/profiling, the problem won't occur.

mvb
  • 701
  • 1
  • 11
  • 20
  • Thank you! How can I simulate pressing the stop button in Xcode, in objective-c? If there is such code I could pace that in the 'applicationWillTerminate' method. – Greg Cawthorne Aug 31 '12 at 21:16
  • @GregCawthorne: I think this question can perhaps solve your queries: http://stackoverflow.com/questions/3097244/exit-application-in-ios-4-0 – mvb Aug 31 '12 at 21:22
  • Furthermore, the Xcode stop button is meant to stop the connection between your simulator/device and the Xcode. Though you can definitely customize your applicationWillTerminate method for unseen circumstances when your application crashes. – mvb Aug 31 '12 at 21:25
  • Thank you. Could you possibly add "This won't happen once your app is installed on a device and not connected to Xcode for running/profiling/testing." to your answer as this, I feel, was the information that satisfied my problem. – Greg Cawthorne Aug 31 '12 at 21:28
  • 2
    @GregCawthorne to simulate the exit, just use `exit(0)` in C :) –  Aug 31 '12 at 21:32