首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重复NSException时NSTask

重复NSException时NSTask
EN

Stack Overflow用户
提问于 2014-03-31 20:13:33
回答 1查看 343关注 0票数 2

我有这样的代码,它应该检查某些设置并记录结果。第一次设置检查工作正常,但下一次检查失败。下面是代码块:

代码语言:javascript
复制
//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设置Enforcer5379:303 *终止应用程序由于非正常异常'NSFileHandleOperationException',原因:' -NSConcreteFileHandle fileDescriptor:没有这样的进程‘**第一个抛出调用堆栈:(0 CoreFoundation 0x00007fff8ce4825c __exceptionPreprocess + 172 1 libobjc.A.dylib 0x00007fff8b075e75 objc_exception_throw + 43 2 CoreFoundation 0x00007fff8ce4810c +NSException :format:+ 204 3 Foundation 0x00007fff8ba9a29d )-NSConcreteFileHandle fileDescriptor + 30 4 Foundation 0x00007fff8bb2fb97 -NSConcreteTask launchWithDictionary:+ 2114 5 SettingsEnforcer 0x0000000100001e1e main + 2126 6 libdyld.dylib 0x00007ff91e555fd start +1) libc++abi.dylib:终止时未发现NSException类型的例外

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-31 20:28:11

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

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22771291

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档