Fixed sgsnemu freepdp on failed create context.
diff --git a/gtp/gtp.c b/gtp/gtp.c
index 7ec08b2..7c4a72f 100644
--- a/gtp/gtp.c
+++ b/gtp/gtp.c
@@ -1554,8 +1554,8 @@
     gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
 		"Invalid message format");
     if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
-    if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
-    pdp_freepdp(pdp);
+    /*    if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
+	  pdp_freepdp(pdp); */
     return EOF;
   }
 
@@ -1565,8 +1565,8 @@
     gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
 		"Missing mandatory information field");
     if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
-    if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
-    pdp_freepdp(pdp);
+    /*    if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
+	  pdp_freepdp(pdp); */
     return EOF;
   }
 
@@ -1590,8 +1590,8 @@
 	gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
 		    "Missing conditional information field");
 	if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
-	if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
-	pdp_freepdp(pdp);
+	/*    if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
+	      pdp_freepdp(pdp); */
 	return EOF;
       }
     }
@@ -1600,9 +1600,9 @@
       gsn->missing++;
       gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
 		  "Missing conditional information field");
-      if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
-      pdp_freepdp(pdp);
       if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
+	/*    if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
+	      pdp_freepdp(pdp); */
       return EOF;
     }
 
@@ -1612,8 +1612,8 @@
 	gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
 		    "Missing conditional information field");
 	if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
-	if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
-	pdp_freepdp(pdp);
+	/*    if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
+	      pdp_freepdp(pdp); */
 	return EOF;
       }
     
@@ -1622,8 +1622,8 @@
 	gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
 		    "Missing conditional information field");
 	if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
-	if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
-	pdp_freepdp(pdp);
+	/*    if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
+	      pdp_freepdp(pdp); */
 	return EOF;
       }
     }
@@ -1634,8 +1634,8 @@
 	gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
 		    "Missing conditional information field");
 	if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
-	if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
-	pdp_freepdp(pdp);
+	/*    if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
+	      pdp_freepdp(pdp); */
 	return EOF;
       }
     
@@ -1644,8 +1644,8 @@
 	gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
 		    "Missing conditional information field");
 	if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
-	if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
-	pdp_freepdp(pdp);
+	/*    if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
+	      pdp_freepdp(pdp); */
 	return EOF;
       }
     }
@@ -1655,8 +1655,8 @@
       gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
 		  "Missing conditional information field");
       if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
-      if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
-      pdp_freepdp(pdp);
+	/*    if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
+	      pdp_freepdp(pdp); */
     }
     
     if (gtpie_gettlv(ie, GTPIE_EUA, 0, &pdp->eua.l,
@@ -1665,8 +1665,8 @@
       gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
 		  "Missing conditional information field");
       if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
-      if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
-      pdp_freepdp(pdp);
+      /*    if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
+	    pdp_freepdp(pdp); */
       return EOF;
     }
     
@@ -1676,8 +1676,8 @@
       gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
 		  "Missing conditional information field");
       if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
-      if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
-      pdp_freepdp(pdp);
+      /*    if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
+	    pdp_freepdp(pdp); */
       return EOF;
     }
     
@@ -1687,8 +1687,8 @@
       gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
 		  "Missing conditional information field");
       if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
-      if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
-      pdp_freepdp(pdp);
+      /*    if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
+	    pdp_freepdp(pdp); */
       return EOF;
     }
 
@@ -1699,8 +1699,8 @@
 	gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
 		    "Missing conditional information field");
 	if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
-	if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
-	pdp_freepdp(pdp);
+	/*    if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
+	      pdp_freepdp(pdp); */
 	return EOF;
       }
     }
@@ -2101,8 +2101,8 @@
     gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
 		"Invalid message format");
     if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
-    if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
-    pdp_freepdp(pdp);
+    /*    if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
+	  pdp_freepdp(pdp); */
     return EOF;
   }
       
@@ -2112,8 +2112,8 @@
     gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
 		"Missing mandatory information field");
     if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
-    if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
-    pdp_freepdp(pdp);
+    /*    if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
+	  pdp_freepdp(pdp); */
     return EOF;
   }
 
@@ -2125,8 +2125,8 @@
   /* Check all conditional information elements */
   if (GTPCAUSE_ACC_REQ != cause) {
     if (gsn->cb_conf) gsn->cb_conf(type, cause, pdp, cbp);
-    if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
-    pdp_freepdp(pdp);
+    /*    if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
+	  pdp_freepdp(pdp); */
     return 0;
   }
   else {
@@ -2143,8 +2143,8 @@
       gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len,
 		  "Missing conditional information field");
       if (gsn->cb_conf) gsn->cb_conf(type, EOF, pdp, cbp);
-      if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
-      pdp_freepdp(pdp);
+      /*    if (gsn->cb_delete_context) gsn->cb_delete_context(pdp);
+	    pdp_freepdp(pdp); */
       return EOF;
     }
 
diff --git a/sgsnemu/sgsnemu.c b/sgsnemu/sgsnemu.c
index 9befbfb..e636f1b 100644
--- a/sgsnemu/sgsnemu.c
+++ b/sgsnemu/sgsnemu.c
@@ -791,6 +791,8 @@
     }
     else {
       state = 0;
+      pdp_freepdp(iph->pdp);
+      iph->pdp = NULL;
       return EOF;
     }
   }
@@ -798,11 +800,15 @@
   if (cause != 128) {
     printf("Received create PDP context response. Cause value: %d\n", cause);
     state = 0;
+    pdp_freepdp(iph->pdp);
+    iph->pdp = NULL;
     return EOF; /* Not what we expected */
   }
 
   if (pdp_euaton(&pdp->eua, &addr)) {
     printf("Received create PDP context response. Cause value: %d\n", cause);
+    pdp_freepdp(iph->pdp);
+    iph->pdp = NULL;
     state = 0;
     return EOF; /* Not a valid IP address */
   }
@@ -943,7 +949,9 @@
     printf("Setting up PDP context #%d\n", n);
     iparr[n].inuse = 1; /* TODO */
 
-    /* Allocated here. Cleaned up in gtp.c:*/
+    /* Allocated here. */
+    /* If create context failes we have to deallocate ourselves. */
+    /* Otherwise it is deallocated gy gtplib */
     pdp_newpdp(&pdp, options.imsi, n, NULL); 
 
     pdp->peer = &iparr[n];