diff --git a/src/libs/3rdparty/net7ssh/src/ne7ssh.cpp b/src/libs/3rdparty/net7ssh/src/ne7ssh.cpp
index 21f5759e80c38331400af487d924213678c476ef..bb1d268582e95b1a90c5ea64d5b95dbeff2f79fa 100644
--- a/src/libs/3rdparty/net7ssh/src/ne7ssh.cpp
+++ b/src/libs/3rdparty/net7ssh/src/ne7ssh.cpp
@@ -301,9 +301,9 @@ int ne7ssh::connectWithPassword (const char *host, const int port,
   uint32 currentRecord, z;
   uint32 channelID;
 
-  ne7ssh_connection* con = new ne7ssh_connection (callbackFunc, callbackArg);
 
   if (!lock()) return -1;
+  ne7ssh_connection* con = new ne7ssh_connection (callbackFunc, callbackArg);
   if (!conCount) connections = (ne7ssh_connection**) malloc (sizeof (ne7ssh_connection*));
   else connections = (ne7ssh_connection**) realloc (connections, sizeof (ne7ssh_connection*) * (conCount + 1));
   connections[conCount++] = con;
@@ -354,8 +354,8 @@ int ne7ssh::connectWithKey (const char* host, const int port,
   uint32 currentRecord, z;
   uint32 channelID;
 
-  ne7ssh_connection* con = new ne7ssh_connection (callbackFunc, callbackArg);
   if (!lock()) return -1;
+  ne7ssh_connection* con = new ne7ssh_connection (callbackFunc, callbackArg);
   if (!conCount) connections = (ne7ssh_connection**) malloc (sizeof (ne7ssh_connection*) * (conCount + 1));
   else connections = (ne7ssh_connection**) realloc (connections, sizeof (ne7ssh_connection*) * (conCount + 1));
   connections[conCount++] = con;
diff --git a/src/libs/3rdparty/net7ssh/src/ne7ssh_error.cpp b/src/libs/3rdparty/net7ssh/src/ne7ssh_error.cpp
index 46173c75a184f543038644bfb034bd570c14819f..acf848bfbe065bd7965c0188c16c3fcaab377c78 100644
--- a/src/libs/3rdparty/net7ssh/src/ne7ssh_error.cpp
+++ b/src/libs/3rdparty/net7ssh/src/ne7ssh_error.cpp
@@ -137,7 +137,11 @@ bool Ne7sshError::push (int32 channel, const char* format, ...)
 
   va_end (args);
 
-  if (!lock()) return false;
+  if (!lock())
+  {
+    free(errStr);
+    return false;
+  }
   if (!memberCount)
   {
     ErrorBuffer = (Error**) malloc (sizeof(Error*));