Fixed bug in client registration when pjsip_endpt_send_request() returns immediate error

git-svn-id: https://svn.pjsip.org/repos/pjproject/trunk@776 74dad513-b988-da41-8d7b-12977e46ad98
diff --git a/pjsip/src/pjsip-ua/sip_reg.c b/pjsip/src/pjsip-ua/sip_reg.c
index cfb59d4..b777048 100644
--- a/pjsip/src/pjsip-ua/sip_reg.c
+++ b/pjsip/src/pjsip-ua/sip_reg.c
@@ -581,6 +581,10 @@
 			event->body.tsx_state.src.rdata : NULL;
 	}
 
+	/* Increment pending_tsx temporarily to prevent regc from
+	 * being destroyed.
+	 */
+	++regc->pending_tsx;
 
 	/* Call callback. */
 	if (expiration == 0xFFFF) expiration = -1;
@@ -590,6 +594,8 @@
 		      rdata, expiration, 
 		      contact_cnt, contact);
 
+	/* Decrement pending_tsx */
+	--regc->pending_tsx;
     }
 
     /* Delete the record if user destroy regc during the callback. */
@@ -623,12 +629,17 @@
     /* Increment pending transaction first, since transaction callback
      * may be called even before send_request() returns!
      */
-    ++regc->pending_tsx;
+    regc->pending_tsx += 2;
     status = pjsip_endpt_send_request(regc->endpt, tdata, -1, regc, &tsx_callback);
     if (status!=PJ_SUCCESS) {
-	--regc->pending_tsx;
 	PJ_LOG(4,(THIS_FILE, "Error sending request, status=%d", status));
     }
+    --regc->pending_tsx;
+
+    /* Delete the record if user destroy regc during the callback. */
+    if (regc->_delete_flag && regc->pending_tsx==0) {
+	pjsip_regc_destroy(regc);
+    }
 
     return status;
 }