Ios NMSShsSession uploadFile EXC_错误访问
我正在使用NMSSH尝试上载文件。我能够成功连接和身份验证,但是上载挂起,并且在uploadFile()函数调用中出现EXC_BAD_访问错误 我知道这通常意味着我试图访问一个不存在的对象,但我尝试使用僵尸剖析器,我可以看到任何突出的东西。此外,我还实现了didDisconnectWithError()和diddreaderror()函数,但从未出现任何错误。上传前我是否遗漏了一步?代码如下:Ios NMSShsSession uploadFile EXC_错误访问,ios,objective-c,libssh2,nmssh,Ios,Objective C,Libssh2,Nmssh,我正在使用NMSSH尝试上载文件。我能够成功连接和身份验证,但是上载挂起,并且在uploadFile()函数调用中出现EXC_BAD_访问错误 我知道这通常意味着我试图访问一个不存在的对象,但我尝试使用僵尸剖析器,我可以看到任何突出的东西。此外,我还实现了didDisconnectWithError()和diddreaderror()函数,但从未出现任何错误。上传前我是否遗漏了一步?代码如下: - (IBAction)sendPressed:(UIButton *)sender { N
- (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]);
}
我不熟悉这个库,但看起来您正在调用异步运行的
uploadFile:to:progress:
,然后立即使用[session disconnect]
断开连接
这可能会导致会话对象被解除分配,并在上载队列尝试通知其进度时崩溃
猜测一下,您需要检查块内的
nsuiger value
值,只有当它达到100%时,您才能安全地断开连接。结果表明,我必须使用会话的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");
}
我将连接、发送和断开连接分为三个不同的功能,分别映射到三个不同的按钮。问题依然存在。我已经更新了原始问题中的代码。您需要将您的
会话
实例保留在@属性
中。尝试声明@property(非原子,强)NHSShsSession*会话
,然后在connectPressed:
中使用self.session=[nmsshsSession connectToHost…]初始化它。
。您的会话对象将在connectPressed:
函数的末尾被释放。因此,我在.h文件中创建了会话作为属性。问题依然存在。如果我连接,然后断开连接,而不尝试发送任何东西,我不会收到任何错误。因此,会话对象不会在创建之后立即解除分配,否则断开连接也会以同样的方式失败。uploadFile()一定有些不同。我认为我正确地遵循了网站上的演示示例[.这似乎很简单,所以我肯定错过了一些明显的东西。Bummer可能尝试上传文件:to:
方法,看看它是否有任何区别。请看:是的,这是我第一次尝试,结果相同。我不是真的与这个特定的库绑定。我只是需要一个iOS库,可以通过SFTP上传文件。如果你是familiar与另一个请让我知道。您仍在使用此代码吗?如果是,如果在上载完成之前断开连接会发生什么情况?您的代码使用NMSFTP.m中调用writeStream
的writeContents
。我的代码使用resumeStream
,但在upl之前断开sftp连接时,它会与EXC\u BAD\u访问崩溃oad已完成。有问题的行libssh2\u sftp\u close(handle)
。修复程序仅在完成上载后调用此命令:if(success)libssh2\u sftp\u close(handle)
。