Merge pull request #165 from kpbarrett/devel

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 7dcaefd..8c09f68 100644
--- a/src/session_client.c
+++ b/src/session_client.c
@@ -97,15 +97,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 b73ae97..f5bd61f 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 4f98d0a..b548dcb 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 4ee1748..b25a4a7 100644
--- a/src/session_p.h
+++ b/src/session_p.h
@@ -707,6 +707,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 */
 
@@ -727,6 +728,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 */