Index: /pjproject/branches/1.x/pjsip/include/pjsua-lib/pjsua_internal.h
===================================================================
--- /pjproject/branches/1.x/pjsip/include/pjsua-lib/pjsua_internal.h	(revision 4388)
+++ /pjproject/branches/1.x/pjsip/include/pjsua-lib/pjsua_internal.h	(revision 4389)
@@ -250,4 +250,7 @@
 
     pj_pool_t		*pool;	    /**< Pool		    */
+    int			 ref_cnt;   /**< Reference count    */
+    pj_bool_t		 destroy_flag; /**< To be destroyed */
+    pj_bool_t		 has_result;
     unsigned		 count;	    /**< # of entries	    */
     pj_str_t		*srv;	    /**< Array of entries   */
Index: /pjproject/branches/1.x/pjsip/src/pjsua-lib/pjsua_core.c
===================================================================
--- /pjproject/branches/1.x/pjsip/src/pjsua-lib/pjsua_core.c	(revision 4388)
+++ /pjproject/branches/1.x/pjsip/src/pjsua-lib/pjsua_core.c	(revision 4389)
@@ -957,9 +957,15 @@
 }
 
-/* Internal function to destroy STUN resolution session
- * (pj_stun_resolve).
- */
+static void stun_resolve_add_ref(pjsua_stun_resolve *sess)
+{
+    ++sess->ref_cnt;
+}
+
 static void destroy_stun_resolve(pjsua_stun_resolve *sess)
 {
+    sess->destroy_flag = PJ_TRUE;
+    if (sess->ref_cnt > 0)
+	return;
+
     PJSUA_LOCK();
     pj_list_erase(sess);
@@ -970,4 +976,12 @@
 }
 
+static void stun_resolve_dec_ref(pjsua_stun_resolve *sess)
+{
+    --sess->ref_cnt;
+    if (sess->ref_cnt <= 0 && sess->destroy_flag)
+	destroy_stun_resolve(sess);
+}
+
+
 /* This is the internal function to be called when STUN resolution
  * session (pj_stun_resolve) has completed.
@@ -976,4 +990,7 @@
 {
     pj_stun_resolve_result result;
+
+    if (sess->has_result)
+	goto on_return;
 
     pj_bzero(&result, sizeof(result));
@@ -982,4 +999,5 @@
     result.name = sess->srv[sess->idx];
     pj_memcpy(&result.addr, &sess->addr, sizeof(result.addr));
+    sess->has_result = PJ_TRUE;
 
     if (result.status == PJ_SUCCESS) {
@@ -997,6 +1015,9 @@
     }
 
+    stun_resolve_add_ref(sess);
     sess->cb(&result);
-
+    stun_resolve_dec_ref(sess);
+
+on_return:
     if (!sess->blocking) {
 	destroy_stun_resolve(sess);
@@ -1060,7 +1081,10 @@
 static void resolve_stun_entry(pjsua_stun_resolve *sess)
 {
+    stun_resolve_add_ref(sess);
+
     /* Loop while we have entry to try */
     for (; sess->idx < sess->count; ++sess->idx) {
 	const int af = pj_AF_INET();
+	char target[64];
 	pj_str_t hostpart;
 	pj_uint16_t port;
@@ -1069,11 +1093,13 @@
 	pj_assert(sess->idx < sess->count);
 
+	pj_ansi_snprintf(target, sizeof(target), "%.*s",
+			 (int)sess->srv[sess->idx].slen,
+			 sess->srv[sess->idx].ptr);
+
 	/* Parse the server entry into host:port */
 	sess->status = pj_sockaddr_parse2(af, 0, &sess->srv[sess->idx],
 					  &hostpart, &port, NULL);
 	if (sess->status != PJ_SUCCESS) {
-	    PJ_LOG(2,(THIS_FILE, "Invalid STUN server entry %.*s", 
-		      (int)sess->srv[sess->idx].slen, 
-		      sess->srv[sess->idx].ptr));
+	    PJ_LOG(2,(THIS_FILE, "Invalid STUN server entry %s", target));
 	    continue;
 	}
@@ -1085,8 +1111,6 @@
 	pj_assert(sess->stun_sock == NULL);
 
-	PJ_LOG(4,(THIS_FILE, "Trying STUN server %.*s (%d of %d)..",
-		  (int)sess->srv[sess->idx].slen,
-		  sess->srv[sess->idx].ptr,
-		  sess->idx+1, sess->count));
+	PJ_LOG(4,(THIS_FILE, "Trying STUN server %s (%d of %d)..",
+		  target, sess->idx+1, sess->count));
 
 	/* Use STUN_sock to test this entry */
@@ -1100,7 +1124,6 @@
 	    pj_strerror(sess->status, errmsg, sizeof(errmsg));
 	    PJ_LOG(4,(THIS_FILE, 
-		     "Error creating STUN socket for %.*s: %s",
-		      (int)sess->srv[sess->idx].slen,
-		      sess->srv[sess->idx].ptr, errmsg));
+		     "Error creating STUN socket for %s: %s",
+		     target, errmsg));
 
 	    continue;
@@ -1113,10 +1136,11 @@
 	    pj_strerror(sess->status, errmsg, sizeof(errmsg));
 	    PJ_LOG(4,(THIS_FILE, 
-		     "Error starting STUN socket for %.*s: %s",
-		      (int)sess->srv[sess->idx].slen,
-		      sess->srv[sess->idx].ptr, errmsg));
-
-	    pj_stun_sock_destroy(sess->stun_sock);
-	    sess->stun_sock = NULL;
+		     "Error starting STUN socket for %s: %s",
+		     target, errmsg));
+
+	    if (sess->stun_sock) {
+		pj_stun_sock_destroy(sess->stun_sock);
+		sess->stun_sock = NULL;
+	    }
 	    continue;
 	}
@@ -1125,5 +1149,5 @@
 	 * stun_sock_cb()
 	 */
-	return;
+	goto on_return;
     }
 
@@ -1134,4 +1158,7 @@
 	stun_resolve_complete(sess);
     }
+
+on_return:
+    stun_resolve_dec_ref(sess);
 }
 
