Index: pjproject/trunk/pjsip/src/pjsip-ua/sip_inv.c
===================================================================
--- pjproject/trunk/pjsip/src/pjsip-ua/sip_inv.c	(revision 2861)
+++ pjproject/trunk/pjsip/src/pjsip-ua/sip_inv.c	(revision 2869)
@@ -78,4 +78,7 @@
 };
 
+#define POOL_INIT_SIZE	256
+#define POOL_INC_SIZE	256
+
 /*
  * Static prototypes.
@@ -235,4 +238,8 @@
 	pjsip_timer_end_session(inv);
 	pjsip_dlg_dec_session(inv->dlg, &mod_inv.mod);
+
+	/* Release the flip-flop pools */
+	pj_pool_release(inv->pool_prov);
+	pj_pool_release(inv->pool_active);
     }
 }
@@ -670,4 +677,12 @@
     inv->cause = (pjsip_status_code) 0;
 
+    /* Create flip-flop pool (see ticket #877) */
+    /* (using inv->obj_name as temporary variable for pool names */
+    pj_ansi_snprintf(inv->obj_name, PJ_MAX_OBJ_NAME, "inv%p", dlg->pool);
+    inv->pool_prov = pjsip_endpt_create_pool(dlg->endpt, inv->obj_name,
+					     POOL_INIT_SIZE, POOL_INC_SIZE);
+    inv->pool_active = pjsip_endpt_create_pool(dlg->endpt, inv->obj_name,
+					       POOL_INIT_SIZE, POOL_INC_SIZE);
+
     /* Object name will use the same dialog pointer. */
     pj_ansi_snprintf(inv->obj_name, PJ_MAX_OBJ_NAME, "inv%p", dlg);
@@ -675,6 +690,6 @@
     /* Create negotiator if local_sdp is specified. */
     if (local_sdp) {
-	status = pjmedia_sdp_neg_create_w_local_offer(dlg->pool, local_sdp,
-						      &inv->neg);
+	status = pjmedia_sdp_neg_create_w_local_offer(inv->pool, 
+						      local_sdp, &inv->neg);
 	if (status != PJ_SUCCESS) {
 	    pjsip_dlg_dec_lock(dlg);
@@ -1130,4 +1145,12 @@
     inv->cause = (pjsip_status_code) 0;
 
+    /* Create flip-flop pool (see ticket #877) */
+    /* (using inv->obj_name as temporary variable for pool names */
+    pj_ansi_snprintf(inv->obj_name, PJ_MAX_OBJ_NAME, "inv%p", dlg->pool);
+    inv->pool_prov = pjsip_endpt_create_pool(dlg->endpt, inv->obj_name,
+					     POOL_INIT_SIZE, POOL_INC_SIZE);
+    inv->pool_active = pjsip_endpt_create_pool(dlg->endpt, inv->obj_name,
+					       POOL_INIT_SIZE, POOL_INC_SIZE);
+
     /* Object name will use the same dialog pointer. */
     pj_ansi_snprintf(inv->obj_name, PJ_MAX_OBJ_NAME, "inv%p", dlg);
@@ -1151,10 +1174,11 @@
     /* Create negotiator. */
     if (rem_sdp) {
-	status = pjmedia_sdp_neg_create_w_remote_offer(inv->pool, local_sdp,
-						       rem_sdp, &inv->neg);
+	status = pjmedia_sdp_neg_create_w_remote_offer(inv->pool, 
+						       local_sdp, rem_sdp, 
+						       &inv->neg);
 						
     } else if (local_sdp) {
-	status = pjmedia_sdp_neg_create_w_local_offer(inv->pool, local_sdp,
-						      &inv->neg);
+	status = pjmedia_sdp_neg_create_w_local_offer(inv->pool, 
+						      local_sdp, &inv->neg);
     } else {
 	status = PJ_SUCCESS;
@@ -1440,4 +1464,12 @@
 
 
+/* Util: swap pool */
+static void swap_pool(pj_pool_t **p1, pj_pool_t **p2)
+{
+    pj_pool_t *tmp = *p1;
+    *p1 = *p2;
+    *p2 = tmp;
+}
+
 /*
  * Initiate SDP negotiation in the SDP negotiator.
@@ -1451,5 +1483,5 @@
 		     PJMEDIA_SDPNEG_EINSTATE);
 
-    status = pjmedia_sdp_neg_negotiate(inv->pool, inv->neg, 0);
+    status = pjmedia_sdp_neg_negotiate(inv->pool_prov, inv->neg, 0);
 
     PJ_LOG(5,(inv->obj_name, "SDP negotiation done, status=%d", status));
@@ -1457,4 +1489,8 @@
     if (mod_inv.cb.on_media_update && inv->notify)
 	(*mod_inv.cb.on_media_update)(inv, status);
+
+    /* Swap the flip-flop pool, and reset the new provisional pool */
+    swap_pool(&inv->pool_prov, &inv->pool_active);
+    pj_pool_reset(inv->pool_prov);
 
     return status;
@@ -1535,5 +1571,6 @@
 
 	    /* Feed the original offer to negotiator */
-	    status = pjmedia_sdp_neg_modify_local_offer(inv->pool, inv->neg,
+	    status = pjmedia_sdp_neg_modify_local_offer(inv->pool_prov, 
+							inv->neg,
 						        reoffer_sdp);
 	    if (status != PJ_SUCCESS) {
@@ -1581,8 +1618,8 @@
 
 	if (inv->neg == NULL) {
-	    status=pjmedia_sdp_neg_create_w_remote_offer(inv->pool, NULL, 
+	    status=pjmedia_sdp_neg_create_w_remote_offer(inv->pool, NULL,
 							 rem_sdp, &inv->neg);
 	} else {
-	    status=pjmedia_sdp_neg_set_remote_offer(inv->pool, inv->neg, 
+	    status=pjmedia_sdp_neg_set_remote_offer(inv->pool_prov, inv->neg, 
 						    rem_sdp);
 	}
@@ -1616,5 +1653,5 @@
 		  pjsip_rx_data_get_info(rdata)));
 
-	status = pjmedia_sdp_neg_set_remote_answer(inv->pool, inv->neg, 
+	status = pjmedia_sdp_neg_set_remote_answer(inv->pool_prov, inv->neg,
 						   rem_sdp);
 
@@ -1669,10 +1706,11 @@
 
 	if (inv->neg == NULL) {
-	    status = pjmedia_sdp_neg_create_w_local_offer(inv->pool, local_sdp,
+	    status = pjmedia_sdp_neg_create_w_local_offer(inv->pool, 
+							  local_sdp,
 							  &inv->neg);
 	} else if (pjmedia_sdp_neg_get_state(inv->neg)==
 		   PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER)
 	{
-	    status = pjmedia_sdp_neg_set_local_answer(inv->pool, inv->neg,
+	    status = pjmedia_sdp_neg_set_local_answer(inv->pool_prov, inv->neg,
 						      local_sdp);
 	} else {
@@ -1879,5 +1917,5 @@
 
     pjsip_dlg_inc_lock(inv->dlg);
-    status = pjmedia_sdp_neg_set_local_answer( inv->pool, inv->neg, sdp);
+    status = pjmedia_sdp_neg_set_local_answer( inv->pool_prov, inv->neg, sdp);
     pjsip_dlg_dec_lock(inv->dlg);
 
@@ -2228,5 +2266,6 @@
     if (new_offer) {
 	if (!inv->neg) {
-	    status = pjmedia_sdp_neg_create_w_local_offer(inv->pool, new_offer,
+	    status = pjmedia_sdp_neg_create_w_local_offer(inv->pool, 
+							  new_offer,
 							  &inv->neg);
 	    if (status != PJ_SUCCESS)
@@ -2247,5 +2286,6 @@
 
 	    case PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER:
-		status = pjmedia_sdp_neg_set_local_answer(inv->pool, inv->neg,
+		status = pjmedia_sdp_neg_set_local_answer(inv->pool_prov, 
+							  inv->neg,
 							  new_offer);
 		if (status != PJ_SUCCESS)
@@ -2260,5 +2300,6 @@
 
 	    case PJMEDIA_SDP_NEG_STATE_DONE:
-		status = pjmedia_sdp_neg_modify_local_offer(inv->pool,inv->neg,
+		status = pjmedia_sdp_neg_modify_local_offer(inv->pool_prov,
+							    inv->neg,
 							    new_offer);
 		if (status != PJ_SUCCESS)
@@ -2316,5 +2357,5 @@
      * with correct SDP origin.
      */
-    status = pjmedia_sdp_neg_modify_local_offer(inv->pool,inv->neg,
+    status = pjmedia_sdp_neg_modify_local_offer(inv->pool_prov, inv->neg,
 						offer);
     if (status != PJ_SUCCESS)
@@ -3745,7 +3786,8 @@
 			 * fix the offer with correct SDP origin.
 			 */
-			status = pjmedia_sdp_neg_modify_local_offer(dlg->pool,
-								    inv->neg,
-								    sdp);
+			status = 
+			    pjmedia_sdp_neg_modify_local_offer(inv->pool_prov,
+							       inv->neg,
+							       sdp);
 
 			/* Retrieve the "fixed" offer from negotiator */
@@ -3760,5 +3802,5 @@
 		if (sdp == NULL) {
 		    const pjmedia_sdp_session *active_sdp = NULL;
-		    status = pjmedia_sdp_neg_send_local_offer(dlg->pool, 
+		    status = pjmedia_sdp_neg_send_local_offer(inv->pool_prov,
 							      inv->neg, 
 							      &active_sdp);
