NSException重复NSTask时

时间:2014-03-31 20:13:33

标签: objective-c

我有这个代码,可以检查某些设置并记录结果。第一个设置检查正常,但下一个设置失败。这是代码块:

//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 __exceptionPreprocess + 172       1 libobjc.A.dylib 0x00007fff8b075e75 objc_exception_throw + 43       2 CoreFoundation 0x00007fff8ce4810c + [NSException raise:format:] + 204       3基础0x00007fff8ba9a29d - [NSConcreteFileHandle fileDescriptor] + 30       4基础0x00007fff8bb2fb97 - [NSConcreteTask launchWithDictionary:] + 2114       5 SettingsEnforcer 0x0000000100001e1e main + 2126       6 libdyld.dylib 0x00007fff91e555fd start + 1   )   libc ++ abi.dylib:以NSException类型的未捕获异常终止

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

1 个答案:

答案 0 :(得分:9)

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