NMSSHSession uploadFile EXC_BAD_ACCESS

时间:2015-02-26 02:42:16

标签: ios objective-c libssh2 nmssh

我正在使用NMSSH尝试上传文件。我能够成功连接和验证,但上传挂起,我在uploadFile()函数调用上得到EXC_BAD_ACCESS错误。

我知道这通常意味着我正在尝试访问一个不存在的对象,但我尝试使用Zombies探查器,我可以看到任何突出的东西。另外,我已经实现了didDisconnectWithError()和didReadError()函数,但我从来没有遇到任何错误。我在上传之前错过了一步吗?这是代码:

- (IBAction)sendPressed:(UIButton *)sender
{

    NMSSHSession *session = [NMSSHSession connectToHost:@"***.com:22" withUsername:@"***"];

    if (session.isConnected) {
        NSLog(@"Connection suceeded");

        [session authenticateByPassword:@"***"];

        if (session.isAuthorized) {
            NSLog(@"Authentication succeeded");

            NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Archive-Small" ofType:@"zip"];
            [session.channel uploadFile:filePath to:@"/test/" progress:^BOOL(NSUInteger value) {

                NSLog(@"Progress: %d", (int)value);

                return YES;
            }];
        }
    }

    [session disconnect];
}

*更新*

@interface SFTPVC ()
{
    NMSSHSession *session;
}
@end

@implementation SFTPVC

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



- (IBAction)connectPressed:(UIButton *)sender
{
    session = [NMSSHSession connectToHost:@"***:22" withUsername:@"***"];

    if (session.isConnected) {
        NSLog(@"Connection suceeded");

        [session authenticateByPassword:@"***"];

        if (session.isAuthorized) {
            NSLog(@"Authentication succeeded");
        }
    }
}

- (IBAction)sendPressed:(UIButton *)sender
{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Archive-Small" ofType:@"zip"];
    [session.channel uploadFile:filePath to:@"/test/" progress:^BOOL(NSUInteger value) {

        NSLog(@"Progress: %d", (int)value);

        return YES;
    }];
}

- (IBAction)disconnectPressed:(UIButton *)sender
{
    [session disconnect];
}

// Session delegates
- (void)session:(NMSSHSession *)session didDisconnectWithError:(NSError *)error
{
    NSLog(@"didDisconnectWithError: %@", [error description]);
}

// Channel delegates
- (void)channel:(NMSSHChannel *)channel didReadError:(NSString *)error
{
    NSLog(@"didReadError: %@", [error description]);
}

2 个答案:

答案 0 :(得分:1)

原来我必须使用会话' sftp而不是频道。这是完整的工作源代码。

- (IBAction)connectPressed:(UIButton *)sender
{

    self.session = [NMSSHSession connectToHost:@"***:22" withUsername:@"***"];

    if (self.session.isConnected) {
        NSLog(@"Connection suceeded");

        [self.session authenticateByPassword:@"***"];

        if (self.session.isAuthorized) {
            NSLog(@"Authentication succeeded");

            self.sftp = [NMSFTP connectWithSession:self.session];
        }
    } else {
        NSLog(@"Failed to connect to service");
    }
}

- (IBAction)sendPressed:(UIButton *)sender
{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"10000000-b" ofType:nil];
    NSData *fileData = [[NSFileManager defaultManager] contentsAtPath:filePath];

    [self.sftp writeContents:fileData toFileAtPath:@"/test/10MB.test" progress:^BOOL(NSUInteger sent) {
        NSLog(@"%d", (int)sent);

        return YES;
    }];
}

- (IBAction)disconnectPressed:(UIButton *)sender
{
    if (self.sftp) {
        [self.sftp disconnect];
        self.sftp = nil;
    }

    if (self.session) {
        [self.session disconnect];
        self.session = nil;
    }

    NSLog(@"Disconnecting");
}

答案 1 :(得分:0)

我不熟悉这个库,但看起来你正在调用异步运行的uploadFile:to:progress:,然后立即与[session disconnect]断开连接。

这可能导致会话对象被释放,并在上载队列尝试通知其进度时随后崩溃。

猜测你需要在你的区块内检查NSUInteger value的值,一旦达到100%,你就可以安全地断开连接。