fixed bug #37405 'err_tcp()' uses already freed 'netconn' object
diff --git a/CHANGELOG b/CHANGELOG
index f7cf259..c502b00 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -66,6 +66,9 @@
 
  ++ Bugfixes:
 
+  2012-09-26: Simon Goldschmidt
+  * api_msg.c: fixed bug #37405 'err_tcp()' uses already freed 'netconn' object
+
   2012-09-26: patch by Henrik Persson
   * dhcp.c: patch #7843 Fix corner case with dhcp timeouts
 
diff --git a/src/api/api_msg.c b/src/api/api_msg.c
index 88afb8f..d4e44b9 100644
--- a/src/api/api_msg.c
+++ b/src/api/api_msg.c
@@ -456,6 +456,14 @@
   if (sys_mbox_trypost(&conn->acceptmbox, newconn) != ERR_OK) {
     /* When returning != ERR_OK, the pcb is aborted in tcp_process(),
        so do nothing here! */
+    /* remove all references to this netconn from the pcb */
+    struct tcp_pcb* pcb = newconn->pcb.tcp;
+    tcp_arg(pcb, NULL);
+    tcp_recv(pcb, NULL);
+    tcp_sent(pcb, NULL);
+    tcp_poll(pcb, NULL, 4);
+    tcp_err(pcb, NULL);
+    /* remove reference from to the pcb from this netconn */
     newconn->pcb.tcp = NULL;
     /* no need to drain since we know the recvmbox is empty. */
     sys_mbox_free(&newconn->recvmbox);