0

In my program I currently use NSTask 5 times, and it all works very well, but I'm tired of having to repeat so much code when it's all so similar, so I tried putting it in a function. Unfortunately it results in a crash on the line: [task launch]. Other than that I can't figure out what's causing the crash as if I use this code outside the function it works perfectly.

The method I am using is as follows:

- (NSString *)performTask: (NSString *)launchPath: (NSString *)argument1: (NSString *)argument2: (NSString *)argument3: (NSString *)argument4: (NSString *)argument5
{
    NSString *resPath = [[NSBundle mainBundle] resourcePath];
    NSTask *task = [[NSTask alloc] init];
    [task setLaunchPath: [NSString stringWithFormat: launchPath, resPath]];
    NSArray *arguments = [NSArray arrayWithObjects: argument1, argument2, argument3, argument4, argument5, nil];
    [task setArguments: arguments];
    NSPipe *pipe = [NSPipe pipe];
    [task setStandardInput:[NSPipe pipe]];
    [task setStandardOutput: pipe];
    NSFileHandle *file = [pipe fileHandleForReading];
    [task launch];
    NSData *data = [file readDataToEndOfFile];
    NSString *status = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
    return status;
}

I really hope this can be fixed, I really cannot see why this crashes.

Thanks in advance everyone.

Cristian
  • 6,765
  • 7
  • 43
  • 64
  • 3
    How does it crash? What's the exit code? Is there an exception? Is there any console output? – 一二三 Jun 17 '12 at 12:27
  • Get to know and love NSParameterAssert – Grady Player Jun 17 '12 at 16:56
  • It doesn't say anything apart from SIGBART, does that help? or where can I find more information about the crash? – Cristian Jun 17 '12 at 22:36
  • I would advise that you expand your question because you're not providing enough information for anyone to realistically help you. This usually leads to a protracted and frustrating round of requests in the comments for more information by other users who **really do want to help you**. For example, include in the question: code snippets/samples, the API you're using, stack traces, screen shots etc. When you've done this, flag to have your question re-opened. Please also take the time to [read this](http://tinyurl.com/so-hints). Thanks. – Kev Jun 17 '12 at 23:48

1 Answers1

0

Check this out it's pretty cool I use it:

https://gist.github.com/1875386

It's also much easier than using arrayWithObjects: for NSTask...

rc

rick
  • 451
  • 3
  • 13