objective-c - NSException 重复 NSTask 时

原文 标签 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] * Terminating app due to uncaught exception 'NSFileHandleOperationException', reason: ' -[NSConcreteFileHandle fileDescriptor]: No such process' ** First throw call stack: ( 0 CoreFoundation 0x00007fff8ce4825c __exceptionPreprocess + 172 1 libobjc.A.dylib 0x00007fff8b075e75 objc_exception_throw + 43 2 CoreFoundation 0x00007fff8ce4810c +[NSException raise:format:] + 204 3 Foundation 0x00007fff8ba9a29d -[NSConcreteFileHandle fileDescriptor] + 30 4 Foundation 0x00007fff8bb2fb97 -[NSConcreteTask launchWithDictionary:] + 2114 5 SettingsEnforcer 0x0000000100001e1e main + 2126 6 libdyld.dylib 0x00007fff91e555fd start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException



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

最佳答案

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

关于objective-c - NSException 重复 NSTask 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22771291/

相关文章:

ios - NSNumber objCType EXC_BAD_ACCESS

ios - 无法从 NSMutableArray 正确检索对象

ios - 将请求从另一个 View 加载到 Web View 中

ios - 应用关闭时如何删除图像缓存?

ios - Xcode 6.0.1新警告

objective-c - 对 JSON 对象进行排序

objective-c - 尝试关闭UIDocument时防止崩溃

objective-c - 用单个对象替换多个数组内容?

iphone - 如何监听 UIButton 状态变化?

iphone - 检查 NSURLConnection 在 iphone 中是否成功?