views:

55

answers:

4

I have an application that checks its command line parameters and stores values in persistent stores. One of those is a password that I don't want sticking around for people to see with 'ps' and friends. The approach I'm currently looking at is to, after I've stored the values I need, relaunch the process without the command line parameters. My naive approach is this, where args[0] is the path to the application:

        NSTask *task = [[NSTask alloc] init];
        [task setLaunchPath:[args objectAtIndex:0]];
        [task launch];
        [task release];
        [NSApp terminate:nil];

The child is run. However, when my app is terminated the child doesn't seem to orphan but gets stuck. Am I just way off on this one?

More info: So it seems that when I call [NSApp terminate:nil] the NSTask that was launched gets stuck, but if I just exit() then it works fine. However, I'm concerned that things that are open (keychain, plist, etc.) will be in a bad state if I do that.

And note that lots of example code out there is about some watchdog-like process that restarts a separate process when needed. I'm trying to restart the current process that's already running from within that same process.

+2  A: 

There are plenty of examples on the web, but this one looks like it has all the code you need. There are more detailed explanations out there, as well.

Carl Norum
I've seen that one, but it looks like it's trying to run a specific method whereas I only want to relaunch the whole application. I probably misunderstood; I'll check it again.
cygnl7
@cygnl7, the method it's calling relaunches the app...
Carl Norum
Ok, so the only problem here now is that I'm trying to launch a new copy of the same process I'm already in. So if I use NSWorkspace to launch the process it sees that what I want to launch is already running and doesn't launch a new one. I'll keep messing with it.
cygnl7
Ok, I get it. The example you linked to is a separate executable that you run from somewhere else and have it restart your original process.
cygnl7
+1  A: 

Use the keychain API for storing passwords. Don't re-invent the wheel.

NSResponder
I am. That's one of the "persistent stores" I mentioned.
cygnl7
+1  A: 

Create an external process that launches yours when it terminates. Then terminate. Launching Cocoa programs with NSTask doesn't work quite right.

Chuck
Yes, I finally got that after reading that example linked above one more time. Since that answer was posted first they get the green check, but you confirmed my suspicions so I had to at least vote you up. Thank you!
cygnl7
+1  A: 

If you're using Sparkle in your project you can use their relaunch code rather than writing your own. I posted about it on my blog last month. http://blog.mbcharbonneau.com/2010/06/03/restart-your-cocoa-application-using-sparkle/

Marc Charbonneau
Yeah, right now I'm not using sparkle but that blog post opened my eyes to the possibility. Thanks!
cygnl7