Good - no problems with that version of the libssh2 library. Are you certain that your failing script uses the very same libssh2 library ?
Next thing to do would be to determine which line of code in your script is producing the error. I don't see anything in the subroutine you posted that should cause that error, so it probably happens before that sub is called.