Re: Cocoa shell works great from PB, crashes as standalone.
Re: Cocoa shell works great from PB, crashes as standalone.
- Subject: Re: Cocoa shell works great from PB, crashes as standalone.
- From: Brendan Younger <email@hidden>
- Date: Tue, 23 Oct 2001 13:45:28 -0400
On Tuesday, October 23, 2001, at 01:04 PM, Chilton Webb wrote:
Hi,
OK, moving right along here, I thought I'd just wrap my server app up
in a Cocoa shell and run it from there. A few minutes in PB and I had
it working. But when time came to dump this on the world, ie move it to
the server box, the app crashes on launch. I've verified that this
happens from the Finder as well, even on the development machine.
I've narrowed it down to the call to my tool (the server itself). If I
comment out the line that launches it, the app works great. If I leave
it in, it crashes, but only from a double-click in the Finder.
Here's the code. The distinct lack of error checking will be remedied
shortly, but in the interim, why is this crashing?
-Chilton
/// code follows ////
#import "ServerStatus.h"
@implementation ServerStatus
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:
(NSApplication *)theApplication {
return YES;
}
- (void)awakeFromNib {
[window makeKeyAndOrderFront:nil];
[self StartServer];
}
- (IBAction)ToggleOff:(id)sender
{
[gameserver terminate];
}
- (IBAction)ToggleOn:(id)sender
{
[self StartServer];
}
- (void)StartServer {
[StartButton setEnabled:NO];
[StopButton setEnabled:YES];
[statusbox setStringValue:@"Server is running. Click 'Stop' to stop
it."];
gameserver=[[NSTask alloc] init];
[gameserver setLaunchPath:@"./TCPServerAlpha"];
[gameserver launch]; //<-- if I comment this out, it works.
}
- (id)init {
self = [super init];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(serverDown:)
name:NSTaskDidTerminateNotification
object:nil];
gameserver = nil;
return self;
}
- (void)serverDown:(NSNotification *)aNotification {
[statusbox setStringValue:@"Server is stopped. Click 'Start' to
make it run."];
[StartButton setEnabled:YES];
[StopButton setEnabled:NO];
[gameserver release];
gameserver=nil;
}
@end
You're running into problems with the current directory. When you
launch something from Project Builder, the current directory is set to
your application's bundle. When you launch something from the Finder,
it's the user's home directory, when you launch something from the
terminal, it's whatever you've most recently set it to.
To remedy this, you can do one of two things. NSFileManager will let
you set the current directory and then you can use ./TCPServerAlpha.
You can also get the path to your application's bundle's absolute path
and then paste TCPServerAlpha onto it. I'd suggest the latter. Use
[[NSBundle mainBundle] bundlePath] and then paste the path to your
server onto that path. Working with absolute paths is a lot easier than
changing the current directory all the time.
Brendan Younger