在NSTask中崩溃

Geo*_*rge 8 macos cocoa

-launch在NSTask 的一次撞车事件中得到了一份报告.

有问题的代码是:

NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/zsh"];
if(ignore)
{
    [task setArguments:@[scriptPath, recordingFolder, Argument]];
}
else
{
    [task setArguments:@[scriptPath, recordingFolder]];
}

NSPipe *outPipe = [NSPipe pipe];
[task setStandardOutput:outPipe];

NSPipe *errorPipe = [NSPipe pipe];
[task setStandardError:errorPipe];

[task launch];
Run Code Online (Sandbox Code Playgroud)

scriptPath是应用程序包中包含的脚本.崩溃说:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Failed to set posix_spawn_file_actions for fd -1 at index 0 with errno 9'
Run Code Online (Sandbox Code Playgroud)

可能是什么原因造成的?posix_spawn_file_actions引用了什么文件描述符?这是否意味着可执行脚本错误或者outPipe或errPipe格式不正确?

我相信它指的是posix_spawn函数:https: //developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/posix_spawn.2.html

并且errno 9是EBADF(错误的文件号).

小智 6

我有一个类似的错误,在我使用下面的命令后,没关系,你可以尝试一下.

NSFileHandle *file=[outPipe fileHandleForReading];
[task launch];
....//read file.
//this is the most important.
[file closeFile];
Run Code Online (Sandbox Code Playgroud)