Objective c 重复NSTask时出现异常

Objective c 重复NSTask时出现异常,objective-c,Objective C,我有一段代码,用来检查某些设置并记录结果。第一次设置检查工作正常,但下一次设置检查失败。以下是代码块: //init classes CocoaLogging *logFile = [[CocoaLogging alloc]init]; NSString * result; NSInteger * state; NSPipe *pipe=[[NSPipe alloc] init]; NSFileHandle *handle; NSString *

我有一段代码,用来检查某些设置并记录结果。第一次设置检查工作正常,但下一次设置检查失败。以下是代码块:

//init classes
    CocoaLogging *logFile = [[CocoaLogging alloc]init];

    NSString * result;
    NSInteger * state;
    NSPipe *pipe=[[NSPipe alloc] init];
    NSFileHandle *handle;
    NSString * cmd = [NSString stringWithFormat:@"%@", @"/usr/bin/defaults"];

    //Start logging
    NSString *logText;
    NSString *logPath;
    BOOL logSuccess;

    NSLog(@"Start Settings Enforcer");

    logPath = [NSString stringWithFormat:@"%@%@", NSHomeDirectory(), LOGFILE_PATH];
    if (! [[NSFileManager defaultManager] fileExistsAtPath:logPath isDirectory:NO])
    {
        logSuccess = [logFile createLogFile:logPath];
        if (logSuccess)
        {
            logText = [NSString stringWithFormat:@"%@", @"Start Settings Enforcer"];
            [logFile makeLogEntry:logText to:logPath];
        }
    }
    else
    {
        logPath = [NSString stringWithFormat:@"%@%@", NSHomeDirectory(), LOGFILE_PATH];

        logText = [NSString stringWithFormat:@"%@", @"Start Settings Enforcer"];
        logSuccess = [logFile makeLogEntry:logText to:logPath];
    }


    //check status of firewall
    //defaults read "/Library/Preferences/com.apple.alf" globalstate
    //array of commandline args
    NSMutableArray *firewallArgs = [[NSMutableArray alloc]initWithObjects:@"-currentHost", @"read", @"/Library/Preferences/com.apple.alf", @"globalstate", nil];

    //init the task
    NSTask *firewall=[[NSTask alloc] init];

    //define the command to run
    [firewall setLaunchPath:cmd];
    [firewall setArguments:firewallArgs];
    [firewall setStandardOutput:pipe];
    handle=[pipe fileHandleForReading];

    //run the command
    [firewall launch];



    // convert NSData -> NSString
    result = [[NSString alloc] initWithData:[handle readDataToEndOfFile]encoding:NSASCIIStringEncoding];
    state = [result intValue];

    NSLog(@"%@", result);
    if (state == 1)
    {
        NSLog(@"firewall is on");
        logText = [NSString stringWithFormat:@"%@", @"firewall is on"];
        logSuccess = [logFile makeLogEntry:logText to:logPath];
    }
    else
    {
        NSLog(@"firewall is off");
        logText = [NSString stringWithFormat:@"%@", @"firewall is off"];
        logSuccess = [logFile makeLogEntry:logText to:logPath];
    }


    [firewallArgs removeAllObjects];
    [handle closeFile];
    [firewall terminate];

    //check screensaver on
    //defaults read ~/Library/Preferences/com.apple.screensaver askForPassword

    NSString * plistPath = [NSString stringWithFormat:@"%@", @"~/Library/Preferences/com.apple.screensaver"];
    plistPath = [plistPath stringByExpandingTildeInPath];

    //array of commandline args
    NSMutableArray *ssOnArgs = [[NSMutableArray alloc]initWithObjects:@"read", plistPath, @"askForPassword", nil];


    //init the task
    NSTask *ssOn=[[NSTask alloc] init];

    //define the command to run
    [ssOn setLaunchPath:cmd];
    [ssOn setArguments:ssOnArgs];
    [ssOn setStandardOutput:pipe];
    handle=[pipe fileHandleForReading];

    //run the command
    [ssOn launch];

    // convert NSData -> NSString
    result = [[NSString alloc] initWithData:[handle readDataToEndOfFile]encoding:NSASCIIStringEncoding];
    state = [result intValue];

    if (state == 1)
    {
        NSLog(@"screensaver is on");
        logText = [NSString stringWithFormat:@"%@", @"screensaver is on"];
        logSuccess = [logFile makeLogEntry:logText to:logPath];
    }
    else
    {
        NSLog(@"screensaver is off");
        logText = [NSString stringWithFormat:@"%@", @"screensaver is off"];
        logSuccess = [logFile makeLogEntry:logText to:logPath];
    }


    NSLog(@"Check-in complete");
    logText = [NSString stringWithFormat:@"%@", @"Check-in complete."];
    logSuccess = [logFile makeLogEntry:logText to:logPath];
}
return 0;
}

以下是错误:

2014-03-31 12:58:11.630 SettingsEnforcer[5379:303]*由于未捕获的异常“NSFileHandleOperationException”而终止应用程序,原因:“-[NSConcreteFileHandle fileDescriptor]:没有这样的进程” **第一次抛出调用堆栈: ( 0 CoreFoundation 0x00007fff8ce4825c例外预处理+172 1 libobjc.A.dylib 0x00007fff8b075e75 objc_异常_抛出+43 2 CoreFoundation 0x00007fff8ce4810c+[N异常提升:格式:][204 3基金会0x000 7FFF8BA9A29 D - [ NStudioFieldFieldFieldSudioTror ] + 30 4基金会0x000 7FFF8BB2FB97 - [ NStrutTeTeTaseAccess词典:] + 2114 5设置传感器0x0000000100001e1e干管+2126 6 libdyld.dylib 0x00007fff91e555fd启动+1 ) libc++abi.dylib:以NSException类型的未捕获异常终止


我希望比我聪明的人能发现错误。

您正在回收
NSPipe
实例。为第二项任务创建新管道。

这对我很有帮助(2015年7月6日)