字符串到字符串 EXC_BAD_ACCESS 为什么?

发布于 2024-11-27 05:48:53 字数 1716 浏览 2 评论 0原文

我错过了什么?

NSString * configPath = nil;
-(IBAction)setPlistPathAndWriteData:(id)sender{   
    NSOpenPanel *panel = [NSOpenPanel openPanel];
    [panel setDirectory:@"/Volumes/"];
    [panel setNameFieldStringValue:@"config.plist"];
    [panel setRequiredFileType:@"plist"];
    NSInteger ret = [panel runModal];

    if ( ret == NSFileHandlingPanelOKButton ) {
        NSString *filePath= [[panel URL] path];
        // with this works fine
        //configPath = [NSString stringWithFormat:@"/Volumes/Macintosh HD/config.plist"];
        // with this EXC BAD ACCESS
        configPath = [NSString stringWithFormat:@"%@", filePath];
        [self writeData];
    }
}

-(void)writeData {
    SET_TEMP_PLIST
    NSTask *task = [[NSTask alloc] init];
    NSPipe *pipe = [[NSPipe alloc] init];
    NSFileHandle *writeHandle = [pipe fileHandleForWriting];
    NSData *configData = [NSPropertyListSerialization dataFromPropertyList:tmpPlist format:
                            NSPropertyListXMLFormat_v1_0 errorDescription:nil];
    [task setLaunchPath:@"/usr/libexec/authopen"];
    ////////////////////////////////////////////////////////EXC_BAD_ACCESS HERE////////
    [task setArguments:[NSArray arrayWithObjects:@"-c", @"-w", configPath, nil]];
    [task setStandardInput:pipe];
    [writeHandle writeData:configData];
    [task launch];
    close([writeHandle fileDescriptor]);
    [task waitUntilExit];
    [task release];
}

编辑

好吧...可以很好地使用此代码:

NSString *filePath= [[[panel URL] path] retain];
const char * cString = [filePath UTF8String];
configPath = [[NSString stringWithUTF8String:cString] retain];

但这不是完美的方法..认为

whats I missed?

NSString * configPath = nil;
-(IBAction)setPlistPathAndWriteData:(id)sender{   
    NSOpenPanel *panel = [NSOpenPanel openPanel];
    [panel setDirectory:@"/Volumes/"];
    [panel setNameFieldStringValue:@"config.plist"];
    [panel setRequiredFileType:@"plist"];
    NSInteger ret = [panel runModal];

    if ( ret == NSFileHandlingPanelOKButton ) {
        NSString *filePath= [[panel URL] path];
        // with this works fine
        //configPath = [NSString stringWithFormat:@"/Volumes/Macintosh HD/config.plist"];
        // with this EXC BAD ACCESS
        configPath = [NSString stringWithFormat:@"%@", filePath];
        [self writeData];
    }
}

-(void)writeData {
    SET_TEMP_PLIST
    NSTask *task = [[NSTask alloc] init];
    NSPipe *pipe = [[NSPipe alloc] init];
    NSFileHandle *writeHandle = [pipe fileHandleForWriting];
    NSData *configData = [NSPropertyListSerialization dataFromPropertyList:tmpPlist format:
                            NSPropertyListXMLFormat_v1_0 errorDescription:nil];
    [task setLaunchPath:@"/usr/libexec/authopen"];
    ////////////////////////////////////////////////////////EXC_BAD_ACCESS HERE////////
    [task setArguments:[NSArray arrayWithObjects:@"-c", @"-w", configPath, nil]];
    [task setStandardInput:pipe];
    [writeHandle writeData:configData];
    [task launch];
    close([writeHandle fileDescriptor]);
    [task waitUntilExit];
    [task release];
}

EDIT

well... works fine with this code:

NSString *filePath= [[[panel URL] path] retain];
const char * cString = [filePath UTF8String];
configPath = [[NSString stringWithUTF8String:cString] retain];

but this is not perfect method.. thought

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果没有 2024-12-04 05:48:53

您的应用程序崩溃可能是因为 configPathnil,稍后在 writeData 中您尝试使用 nil 初始化一个新数组作为第三个对象:

[NSArray arrayWithObjects:@"-c", @"-w", configPath, nil]
//                                          ^^^ this is nil

我建议您复制 path 返回的字符串:

if(ret == NSFileHandlingPanelOKButton ) {
    // you become the owner of the string,
    // don't forget to release configPath later
    configPath = [[[panel URL] path] copy];
    [self writeData];
}

这可能运行没有任何问题。

Your app crashes likely because configPath is nil, and later in writeData you try to initialize a new array with nil as the third object :

[NSArray arrayWithObjects:@"-c", @"-w", configPath, nil]
//                                          ^^^ this is nil

I suggest you copy the string returned by path :

if(ret == NSFileHandlingPanelOKButton ) {
    // you become the owner of the string,
    // don't forget to release configPath later
    configPath = [[[panel URL] path] copy];
    [self writeData];
}

This may run without any problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文