Index: pjproject/branches/projects/uwp/pjsip/src/pjsip/sip_dialog.c
===================================================================
--- pjproject/branches/projects/uwp/pjsip/src/pjsip/sip_dialog.c	(revision 5208)
+++ pjproject/branches/projects/uwp/pjsip/src/pjsip/sip_dialog.c	(revision 5513)
@@ -93,4 +93,10 @@
     pj_list_init(&dlg->rem_cap_hdr);
 
+    /* Init client authentication session. */
+    status = pjsip_auth_clt_init(&dlg->auth_sess, dlg->endpt,
+				 dlg->pool, 0);
+    if (status != PJ_SUCCESS)
+	goto on_error;
+
     status = pj_mutex_create_recursive(pool, dlg->obj_name, &dlg->mutex_);
     if (status != PJ_SUCCESS)
@@ -120,4 +126,5 @@
 	pj_bzero(&dlg->tp_sel, sizeof(pjsip_tpselector));
     }
+    pjsip_auth_clt_deinit(&dlg->auth_sess);
     pjsip_endpt_release_pool(dlg->endpt, dlg->pool);
 }
@@ -283,10 +290,4 @@
     pj_list_init(&dlg->route_set);
 
-    /* Init client authentication session. */
-    status = pjsip_auth_clt_init(&dlg->auth_sess, dlg->endpt,
-				 dlg->pool, 0);
-    if (status != PJ_SUCCESS)
-	goto on_error;
-
     /* Register this dialog to user agent. */
     status = pjsip_ua_register_dlg( ua, dlg );
@@ -312,8 +313,9 @@
  * Create UAS dialog.
  */
-PJ_DEF(pj_status_t) pjsip_dlg_create_uas(   pjsip_user_agent *ua,
-					    pjsip_rx_data *rdata,
-					    const pj_str_t *contact,
-					    pjsip_dialog **p_dlg)
+pj_status_t create_uas_dialog( pjsip_user_agent *ua,
+			       pjsip_rx_data *rdata,
+			       const pj_str_t *contact,
+			       pj_bool_t inc_lock,
+			       pjsip_dialog **p_dlg)
 {
     pj_status_t status;
@@ -505,9 +507,9 @@
     dlg->route_set_frozen = PJ_TRUE;
 
-    /* Init client authentication session. */
-    status = pjsip_auth_clt_init(&dlg->auth_sess, dlg->endpt,
-				 dlg->pool, 0);
-    if (status != PJ_SUCCESS)
-	goto on_error;
+    /* Increment the dialog's lock since tsx may cause the dialog to be
+     * destroyed prematurely (such as in case of transport error).
+     */
+    if (inc_lock)
+        pjsip_dlg_inc_lock(dlg);
 
     /* Create UAS transaction for this request. */
@@ -553,6 +555,38 @@
     }
 
-    destroy_dialog(dlg, PJ_FALSE);
+    if (inc_lock) {
+        pjsip_dlg_dec_lock(dlg);
+    } else {
+        destroy_dialog(dlg, PJ_FALSE);
+    }
+    
     return status;
+}
+
+
+#if !DEPRECATED_FOR_TICKET_1902
+/*
+ * Create UAS dialog.
+ */
+PJ_DEF(pj_status_t) pjsip_dlg_create_uas(   pjsip_user_agent *ua,
+					    pjsip_rx_data *rdata,
+					    const pj_str_t *contact,
+					    pjsip_dialog **p_dlg)
+{
+    return create_uas_dialog(ua, rdata, contact, PJ_FALSE, p_dlg);
+}
+#endif
+
+
+/*
+ * Create UAS dialog and increase its session count.
+ */
+PJ_DEF(pj_status_t)
+pjsip_dlg_create_uas_and_inc_lock(    pjsip_user_agent *ua,
+				      pjsip_rx_data *rdata,
+				      const pj_str_t *contact,
+				      pjsip_dialog **p_dlg)
+{
+    return create_uas_dialog(ua, rdata, contact, PJ_TRUE, p_dlg);
 }
 
@@ -749,9 +783,11 @@
     PJ_ASSERT_RETURN(dlg->tsx_count==0, PJ_EINVALIDOP);
 
-    /* Unregister from user agent. */
-    status = pjsip_ua_unregister_dlg(dlg->ua, dlg);
-    if (status != PJ_SUCCESS) {
-	pj_assert(!"Unexpected failed unregistration!");
-	return status;
+    /* Unregister from user agent, if it has been registered (see #1924) */
+    if (dlg->dlg_set) {
+	status = pjsip_ua_unregister_dlg(dlg->ua, dlg);
+	if (status != PJ_SUCCESS) {
+	    pj_assert(!"Unexpected failed unregistration!");
+	    return status;
+	}
     }
 
@@ -1089,4 +1125,7 @@
 	return status;
 
+    /* Put this dialog in tdata's mod_data */
+    tdata->mod_data[dlg->ua->id] = dlg;
+
     /* Just copy dialog route-set to Route header.
      * The transaction will do the processing as specified in Section 12.2.1
@@ -1188,4 +1227,7 @@
     /* Lock and increment session */
     pjsip_dlg_inc_lock(dlg);
+
+    /* Put this dialog in tdata's mod_data */
+    tdata->mod_data[dlg->ua->id] = dlg;
 
     /* If via_addr is set, use this address for the Via header. */
@@ -1370,4 +1412,7 @@
     /* Lock the dialog. */
     pjsip_dlg_inc_lock(dlg);
+
+    /* Put this dialog in tdata's mod_data */
+    tdata->mod_data[dlg->ua->id] = dlg;
 
     dlg_beautify_response(dlg, PJ_FALSE, st_code, tdata);
