From 2fe7c5c1fb509b7886098afe0196c61751ad38c8 Mon Sep 17 00:00:00 2001
From: Ivica Ico Bukvic <ico@vt.edu>
Date: Wed, 17 Jul 2013 00:13:45 -0400
Subject: [PATCH] fixed segfault with netsend/receive using tcp packets where
 netreceive is deleted while netreceive is connected to it (after which
 sending data to netreceive would result in a segfault)

---
 pd/src/x_net.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/pd/src/x_net.c b/pd/src/x_net.c
index 9d1aee0b1..62cf656ee 100644
--- a/pd/src/x_net.c
+++ b/pd/src/x_net.c
@@ -188,6 +188,7 @@ typedef struct _netreceive
     t_object x_obj;
     t_outlet *x_msgout;
     t_outlet *x_connectout;
+    int x_acceptsocket;
     int x_connectsocket;
     int x_nconnections;
     int x_udp;
@@ -244,6 +245,7 @@ static void netreceive_connectpoll(t_netreceive *x)
             (t_socketnotifier)netreceive_notify,
                 (x->x_msgout ? netreceive_doit : 0), 0);
         sys_addpollfn(fd, (t_fdpollfn)socketreceiver_read, y);
+		x->x_acceptsocket = fd;
         outlet_float(x->x_connectout, ++x->x_nconnections);
     }
 }
@@ -308,6 +310,8 @@ static void *netreceive_new(t_symbol *compatflag,
     }
     else x->x_msgout = outlet_new(&x->x_obj, &s_anything);
 
+	x->x_acceptsocket = -1;
+
     if (udp)        /* datagram protocol */
     {
         t_socketreceiver *y = socketreceiver_new((void *)x, 
@@ -343,6 +347,8 @@ static void netreceive_free(t_netreceive *x)
     if (x->x_connectsocket >= 0)
     {
         sys_rmpollfn(x->x_connectsocket);
+		if (x->x_acceptsocket >= 0)
+			sys_rmpollfn(x->x_acceptsocket);
         sys_closesocket(x->x_connectsocket);
     }
 }
-- 
GitLab