please see attached.

tcpclient was spawning a new thread every time you pressed connect
regardless whether you are connected or not. Hence, crashes.

Still need to test second bug.

Ico
--- tcpclient.c.orig	2010-05-05 18:11:39.000000000 -0400
+++ tcpclient.c	2010-05-05 18:11:56.000000000 -0400
@@ -160,11 +160,15 @@
     struct hostent      *hp;
     int                 sockfd;
 
+	/* Ivica Ico Bukvic <i...@bukvic.net>
+	   not necessary here any more as this is now checked
+	   before the thread is spawned
     if (x->x_fd >= 0)
     {
         error("%s_connect: already connected", objName);
         return (x);
     }
+	*/
 
     /* create a socket */
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
@@ -210,14 +214,22 @@
 
 static void tcpclient_connect(t_tcpclient *x, t_symbol *hostname, t_floatarg fportno)
 {
-    /* we get hostname and port and pass them on
-       to the child thread that establishes the connection */
-    x->x_hostname = hostname->s_name;
-    x->x_port = fportno;
-    x->x_connectstate = 0;
-    /* start child thread */
-    if(pthread_create(&x->x_threadid, &x->x_threadattr, tcpclient_child_connect, x) < 0)
-        post("%s: could not create new thread", objName);
+	/* Ivica Ico Bukvic 5/5/10 <i...@bukvic.net>
+	   first check if we are already connected to avoid
+	   spawning redundant thread and possibly crashing pd */
+	if (x->x_connectstate != 1) {
+		/* we get hostname and port and pass them on
+		   to the child thread that establishes the connection */
+		x->x_hostname = hostname->s_name;
+		x->x_port = fportno;
+		x->x_connectstate = 0;
+		/* start child thread */
+		if(pthread_create(&x->x_threadid, &x->x_threadattr, tcpclient_child_connect, x) < 0)
+		    post("%s: could not create new thread", objName);
+	}
+	else {
+		error("%s_connect: already connected", objName);
+	}
 }
 
 static void tcpclient_disconnect(t_tcpclient *x)
_______________________________________________
Pd-dev mailing list
Pd-dev@iem.at
http://lists.puredata.info/listinfo/pd-dev

Reply via email to