Merge pull request #16 from sswen/duplex-13

[valgrind] mem-leak: free struct attribute memory before parent struc…

reviewed, build tested
diff --git a/src/clnt_vc.c b/src/clnt_vc.c
index c951dcf..174e7e2 100644
--- a/src/clnt_vc.c
+++ b/src/clnt_vc.c
@@ -293,23 +293,32 @@
 	return (clnt);
 
  err:
+	/* XXX fix */
+	if (cs) {
+		mem_free(cs, sizeof(struct ct_serialized));
+	}
+
 	if (clnt) {
-		/* XXX fix */
-		if (xd) {
-			if (ct->ct_addr.len)
-				mem_free(ct->ct_addr.buf, ct->ct_addr.len);
-			free_x_vc_data(xd);
-		}
-		if (cs) {
-			mem_free(cs, sizeof(struct ct_serialized));
-		}
 		mutex_destroy(&clnt->cl_lock);
 		mem_free(clnt, sizeof(CLIENT));
 	}
+
 	if (rec) {
 		if (rpc_dplx_unref(rec, RPC_DPLX_FLAG_LOCKED))
 			REC_UNLOCK(rec);
 	}
+
+	if (xd) {
+		if (ct->ct_addr.len)
+			mem_free(ct->ct_addr.buf, ct->ct_addr.len);
+
+		if (xd->refcnt == 1) {
+			XDR_DESTROY(&xd->shared.xdrs_in);
+			XDR_DESTROY(&xd->shared.xdrs_out);
+			free_x_vc_data(xd);
+		}
+	}
+
 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
 	return (NULL);
 }
diff --git a/src/rpc_dplx.c b/src/rpc_dplx.c
index d0ba382..bf93e27 100644
--- a/src/rpc_dplx.c
+++ b/src/rpc_dplx.c
@@ -349,6 +349,10 @@
 
 	if (rec->refcnt == 0) {
 		t = rbtx_partition_of_scalar(&rpc_dplx_rec_set.xt, rec->fd_k);
+		if (rec->hdl.xd) {
+			rec->hdl.xd->refcnt--;
+			rec->hdl.xd->rec = NULL;
+		}
 		REC_UNLOCK(rec);
 		rwlock_wrlock(&t->lock);
 		nv = opr_rbtree_lookup(&t->t, &rec->node_k);