Fix issue where thread specific data is dereferenced inside the key value destructor
diff --git a/src/session_client.c b/src/session_client.c
index 7dec70f..ac85029 100644
--- a/src/session_client.c
+++ b/src/session_client.c
@@ -92,15 +92,25 @@
 #endif
     {
         /* for the main thread the same is done in nc_client_destroy() */
-        nc_client_set_schema_searchpath(NULL);
+        free(c->opts.schema_searchpath);
+
 #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
-        nc_client_ch_del_bind(NULL, 0, 0);
+        int i;
+        for (i = 0; i < c->opts.ch_bind_count; ++i) {
+            close(c->opts.ch_binds[i].sock);
+            free((char *)c->opts.ch_binds[i].address);
+        }
+        free(c->opts.ch_binds);
+        c->opts.ch_binds = NULL;
+        c->opts.ch_bind_count = 0;
 #endif
 #ifdef NC_ENABLED_SSH
-        nc_client_ssh_destroy_opts();
+        _nc_client_ssh_destroy_opts(&c->ssh_opts);
+        _nc_client_ssh_destroy_opts(&c->ssh_ch_opts);
 #endif
 #ifdef NC_ENABLED_TLS
-        nc_client_tls_destroy_opts();
+        _nc_client_tls_destroy_opts(&c->tls_opts);
+        _nc_client_tls_destroy_opts(&c->tls_ch_opts);
 #endif
         free(c);
     }
diff --git a/src/session_client_ssh.c b/src/session_client_ssh.c
index 8f76e49..40b00df 100644
--- a/src/session_client_ssh.c
+++ b/src/session_client_ssh.c
@@ -152,7 +152,7 @@
     }
 }
 
-static void
+void
 _nc_client_ssh_destroy_opts(struct nc_client_ssh_opts *opts)
 {
     int i;
diff --git a/src/session_client_tls.c b/src/session_client_tls.c
index 616d9ad..9b341cb 100644
--- a/src/session_client_tls.c
+++ b/src/session_client_tls.c
@@ -234,7 +234,7 @@
 
 #endif
 
-static void
+void
 _nc_client_tls_destroy_opts(struct nc_client_tls_opts *opts)
 {
     free(opts->cert_path);
diff --git a/src/session_p.h b/src/session_p.h
index 367cd29..4afb14f 100644
--- a/src/session_p.h
+++ b/src/session_p.h
@@ -694,6 +694,7 @@
 void nc_server_ssh_clear_opts(struct nc_server_ssh_opts *opts);
 
 void nc_client_ssh_destroy_opts(void);
+void _nc_client_ssh_destroy_opts(struct nc_client_ssh_opts *opts);
 
 #endif /* NC_ENABLED_SSH */
 
@@ -714,6 +715,7 @@
 void nc_server_tls_clear_opts(struct nc_server_tls_opts *opts);
 
 void nc_client_tls_destroy_opts(void);
+void _nc_client_tls_destroy_opts(struct nc_client_tls_opts *opts);
 
 #endif /* NC_ENABLED_TLS */