Objective c 使用libssh2执行错误访问?

Objective c 使用libssh2执行错误访问?,objective-c,ios,Objective C,Ios,我终于能够编译libssh2了,但现在我一直看到EXC\u BAD\u ACCESS错误 这是关于libssh2如何工作的基本背景(至少是GUI)这是 输入命令 输入主机IP 输入用户名/密码 按下按钮发送SSH命令 现在,像echo Hello World这样的简单命令可以很好地工作,但作为一个项目,它的全部思想是能够远程重启我的mac。最初,使用命令shutdown-r导致远程计算机上没有任何事情发生,并导致应用程序崩溃。然后我想到,我可以通过使用open/path/to/restart.a

我终于能够编译libssh2了,但现在我一直看到
EXC\u BAD\u ACCESS
错误

这是关于libssh2如何工作的基本背景(至少是GUI)这是

  • 输入命令
  • 输入主机IP
  • 输入用户名/密码
  • 按下按钮发送SSH命令
  • 现在,像
    echo Hello World
    这样的简单命令可以很好地工作,但作为一个项目,它的全部思想是能够远程重启我的mac。最初,使用命令
    shutdown-r
    导致远程计算机上没有任何事情发生,并导致应用程序崩溃。然后我想到,我可以通过使用
    open/path/to/restart.app
    命令简单地打开一个applescript应用程序,重新启动mac,该命令运行良好。机器重新启动了,哎呀

    除此之外,应用程序崩溃并出现以下错误:

    Thread 1: EXC_BAD_ACCESS (code=1, address=_____)
    
    我对objective-c非常陌生,我不知道如何着手解决这个问题。在谷歌上查找,我发现这是由以下原因造成的:

  • 内存问题
  • 消息被发送到一个已经发布的对象(我不确定这意味着什么,有什么帮助吗?)
  • 因此,我猜一旦通过SSH发送命令,它就会断开连接,然后显示结果,但对象已经不存在了

    我会发布这方面的源代码,但它有很多不同的文件,所以我会发布github链接:

    我没有修改任何代码,因此如果有人想复制我的问题,我使用的代码与那里的代码相同

    事故日志:

    Process:         libssh2-for-iOS [38153]
    Path:            /Users/USER/Library/Application Support/iPhone Simulator/*/libssh2-for-iOS.app/libssh2-for-iOS
    Identifier:      libssh2-for-iOS
    Version:         ???
    Code Type:       X86 (Native)
    Parent Process:  ??? [1]
    User ID:         501
    
    Date/Time:       2012-04-18 18:32:03.883 -0400
    OS Version:      Mac OS X 10.8 (12A154q)
    Report Version:  10
    
    Crashed Thread:  0
    
    Exception Type:  EXC_BAD_ACCESS (SIGBUS)
    Exception Codes: KERN_PROTECTION_FAILURE at 0x0000000000000000
    
    External Modification Warnings:
    Debugger attached to process.
    
    VM Regions Near 0:
    --> __PAGEZERO             0000000000000000-0000000000001000 [    4K] ---/--- SM=NUL  /Users/USER/Library/Application Support/iPhone Simulator/*/libssh2-for-iOS.app/libssh2-for-iOS
        __TEXT                 0000000000001000-0000000000002000 [    4K] r-x/rwx SM=COW  /Users/USER/Library/Application Support/iPhone Simulator/*/libssh2-for-iOS.app/libssh2-for-iOS
    
    Application Specific Information:
    iPhone Simulator 272, iPhone OS 5.0 (iPhone/9A334)
    
    
    Thread 0 Crashed:
    0   ???                             0000000000 0 + 0
    1   libssh2-for-iOS                 0x00003f39 _libssh2_channel_open + 281
    2   libssh2-for-iOS                 0x00004837 libssh2_channel_open_ex + 151
    3   libssh2-for-iOS                 0x00003714 -[SSHWrapper executeCommand:] + 212
    4   libssh2-for-iOS                 0x00002b22 -[libssh2_for_iOSAppDelegate executeCommand:] + 338
    5   CoreFoundation                  0x01603ec9 -[NSObject performSelector:withObject:withObject:] + 73
    6   UIKit                           0x0049e5c2 -[UIApplication sendAction:to:from:forEvent:] + 96
    7   UIKit                           0x0049e55a -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
    8   UIKit                           0x00543b76 -[UIControl sendAction:to:forEvent:] + 66
    9   UIKit                           0x0054403f -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 503
    10  UIKit                           0x00542e22 -[UIControl touchesBegan:withEvent:] + 264
    11  UIKit                           0x004c393f -[UIWindow _sendTouchesForEvent:] + 272
    12  UIKit                           0x004c3c56 -[UIWindow sendEvent:] + 273
    13  UIKit                           0x004aa384 -[UIApplication sendEvent:] + 464
    14  UIKit                           0x0049daa9 _UIApplicationHandleEvent + 8196
    15  GraphicsServices                0x01cf8fa9 PurpleEventCallback + 1274
    16  CoreFoundation                  0x015d61c5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
    17  CoreFoundation                  0x0153b022 __CFRunLoopDoSource1 + 146
    18  CoreFoundation                  0x0153990a __CFRunLoopRun + 2218
    19  CoreFoundation                  0x01538db4 CFRunLoopRunSpecific + 212
    20  CoreFoundation                  0x01538ccb CFRunLoopRunInMode + 123
    21  GraphicsServices                0x01cf7879 GSEventRunModal + 207
    22  GraphicsServices                0x01cf793e GSEventRun + 114
    23  UIKit                           0x0049ba9b UIApplicationMain + 1175
    24  libssh2-for-iOS                 0x00003266 main + 134
    25  libssh2-for-iOS                 0x00002955 start + 53
    
    Thread 1:
    0   libsystem_kernel.dylib          0x921059be kevent + 10
    1   libdispatch.dylib               0x01c9e398 _dispatch_mgr_invoke + 918
    2   libdispatch.dylib               0x01c9cc9d _dispatch_mgr_thread + 53
    
    Thread 2:: WebThread
    0   libsystem_kernel.dylib          0x921027e2 mach_msg_trap + 10
    1   libsystem_kernel.dylib          0x92101cc0 mach_msg + 68
    2   CoreFoundation                  0x015d613a __CFRunLoopServiceMachPort + 186
    3   CoreFoundation                  0x01539605 __CFRunLoopRun + 1445
    4   CoreFoundation                  0x01538db4 CFRunLoopRunSpecific + 212
    5   CoreFoundation                  0x01538ccb CFRunLoopRunInMode + 123
    6   WebCore                         0x03583220 RunWebThread(void*) + 560
    7   libsystem_c.dylib               0x9445dd03 _pthread_start + 344
    8   libsystem_c.dylib               0x944486ae thread_start + 34
    
    Thread 0 crashed with X86 Thread State (32-bit):
      eax: 0x00000000  ebx: 0x00000007  ecx: 0x07439400  edx: 0x00000000
      edi: 0x00040000  esi: 0x00008000  ebp: 0xbfffbbc8  esp: 0xbfffbb2c
       ss: 0x00000023  efl: 0x00010286  eip: 0x00000000   cs: 0x0000001b
       ds: 0x00000023   es: 0x00000023   fs: 0x00000000   gs: 0x0000000f
      cr2: 0x00000000
    Logical CPU: 1
    
    (Binary images removed, too many characters)
    
    External Modification Summary:
      Calls made by other processes targeting this process:
        task_for_pid: 1
        thread_create: 0
        thread_set_state: 32
      Calls made by this process:
        task_for_pid: 0
        thread_create: 0
        thread_set_state: 0
      Calls made by all processes on this machine:
        task_for_pid: 14309
        thread_create: 0
        thread_set_state: 550
    
    VM Region Summary:
    ReadOnly portion of Libraries: Total=155.0M resident=50.0M(32%) swapped_out_or_unallocated=105.0M(68%)
    Writable regions: Total=36.7M written=2264K(6%) resident=9020K(24%) swapped_out=108K(0%) unallocated=27.9M(76%)
    
    REGION TYPE                      VIRTUAL
    ===========                      =======
    CG image                             16K
    CoreAnimation                       532K
    MALLOC                             21.8M
    MALLOC guard page                    32K
    Memory tag=35                      10.8M
    SQLite page cache                   288K
    Stack                              65.3M
    VM_ALLOCATE                         136K
    __DATA                             9660K
    __LINKEDIT                         55.9M
    __PAGEZERO                            4K
    __TEXT                             99.1M
    __UNICODE                           544K
    mapped file                       237.8M
    shared memory                        12K
    ===========                      =======
    TOTAL                             501.6M
    

    我已经从下载了示例代码,编译了libssl&libssh2和应用程序。当我运行该应用程序时,我得到了一个与您非常相似的错误:

    Thread 1: EXC_BAD_ACCESS (code=2, address=0x12c)
    
    这种情况发生在我身上,因为某种原因连接没有建立。例如,如果您未输入任何IP地址,则会发生此异常,或者如果您输入的内容不是IP地址,则会发生此异常

    问题似乎在x2on的应用程序中,而不是在libssh2或libssl中。当我调试应用程序时,我发现,当无法建立连接时,名为
    session
    的变量为NULL。因此,稍后对
    libssh2\u channel\u open\u session
    的调用将因此异常而失败,因为它希望其参数不为NULL

    请参见以下屏幕截图:

    在左下角,您可以看到
    会话
    变量的值。如果它为NULL,那么这就是问题所在。您还可以在右下角的console output窗口中看到问题的原因,在我的例子中是“连接失败!”


    无论如何,x2on的代码似乎是一个非常非常糟糕的代码示例,它忽略了错误,使用了糟糕的编码风格和许多不正确的技术(全局变量,而不是设置rootviewcontroller)以及其他不好的东西

    x2on代码有很多错误;一对一让我们检查executeCommand例程:

    • 它完全忽略从服务器返回的缓冲区的数据长度:返回的文本没有尾随零,所以如果您对该例程执行多个调用,您将得到尾随垃圾。解决方案:使用rc1值获取其长度(见下文)

    • 它循环直到接收到所有数据,但它不会在结果字符串中累积这些数据,因此您将只获得接收到的最新文本,而不是完整的服务器答案。解决方案:使用NSMutableString而不是NSString,并将结果附加到其中:

      NSMutableString *result = [[NSMutableString alloc] initWithString: @""];
      ...
      if( rc1 > 0 ) {
          [result appendString: [[NSString alloc] initWithCString:buffer length: rc1]];
              bytecount += rc1;
          }
      
    • *结果处理不正确;如果您对例程进行多次调用,则从第二次调用开始,您将得到一个异常。快速解决方法:

      if (result) return result;
      else return @"Error - command failed";
      

    您是否在调试器中运行应用程序?这会给你一个stacktrace,它会给你更多的线索,告诉你问题出在哪里。它不是正常的调试吗?或者我还需要采取另一个步骤吗?因此,此崩溃发生在调试运行期间,而您没有得到stacktrace?通常情况下,它会崩溃并打开调试器。以前我被告知要进行stacktrace,但我从未找到关于如何捕获它以及显示哪些结果的明确说明。一旦我弄明白了,我会很乐意贴出来的。。。如上所述,您可以看到
    libssh2\u channel\u open()
    正在取消对空指针的引用,因此您可能会将其中一个参数传递为空。在调用
    libssh2\u channel\u open\u ex()
    时插入断点,并在参数进入时检查参数;或者在通话前添加断言,以确保事情如您所期望的那样(一个更好的长期解决方案)。我知道,在通话中感谢他人是不可取的,但感谢您提供了非常全面的回复。这是我能找到的唯一一个允许我从iPad向远程计算机发送SSH命令的例子,你知道有哪些使用更好编码风格的吗?好吧,我建议你编写自己的客户端应用程序。您可以从C中的示例开始,但这并不重要,您可以在Obj-C旁边编译C代码。也有使用非阻塞套接字的示例(x2on的代码不使用,这是另一个问题),因此,在发送或接收数据时,您的应用程序不会被阻止。而且很明显,x2on也会基于libssh2示例构建他的示例应用程序。但是他将代码分成了几个方法,这打破了错误处理。我将读完我关于Obj-C的书,然后开始这个项目。我已经完成了他的代码的非阻塞版本。等待github的分支很快出现。我将NSMutableString声明放在方法的开头,紧跟在“if(![ServerConnectionStatus]){..}检查之后。第二部分(“if(rc1>0)…”)替换了do..while循环中的类似检查。请记住,“NSString stringWithCString:length:”已被弃用,现在您应该使用“NSString stringWithCString:encoding:”,但它没有“lenght:”参数。我会在将来解决这个问题,现在我需要的功能工作。为了完整起见,原始行:
    result=[nsstringwithcstring:bufferencoding:4]