Index: pjproject/trunk/pjsip/include/pjsip/sip_auth.h
===================================================================
--- pjproject/trunk/pjsip/include/pjsip/sip_auth.h	(revision 5335)
+++ pjproject/trunk/pjsip/include/pjsip/sip_auth.h	(revision 5336)
@@ -167,4 +167,5 @@
     PJ_DECL_LIST_MEMBER(struct pjsip_cached_auth);
 
+    pj_pool_t			*pool;      /**< Pool for cached auth	    */
     pj_str_t			 realm;	    /**< Realm.			    */
     pj_bool_t			 is_proxy;  /**< Server type (401/407)	    */
Index: pjproject/trunk/pjsip/include/pjsip/sip_config.h
===================================================================
--- pjproject/trunk/pjsip/include/pjsip/sip_config.h	(revision 5335)
+++ pjproject/trunk/pjsip/include/pjsip/sip_config.h	(revision 5336)
@@ -1123,4 +1123,14 @@
 #endif
 
+/**
+ * Maximum size of pool allowed for auth client session in pjsip_regc.
+ * After the size exceeds because of Digest authentication processing,
+ * the pool is reset.
+ *
+ * Default is 20 kB
+ */
+#ifndef PJSIP_AUTH_CACHED_POOL_MAX_SIZE
+#   define PJSIP_AUTH_CACHED_POOL_MAX_SIZE	(20 * 1024)
+#endif
 
 /*****************************************************************************
Index: pjproject/trunk/pjsip/src/pjsip-ua/sip_reg.c
===================================================================
--- pjproject/trunk/pjsip/src/pjsip-ua/sip_reg.c	(revision 5335)
+++ pjproject/trunk/pjsip/src/pjsip-ua/sip_reg.c	(revision 5336)
@@ -177,4 +177,5 @@
 	pj_lock_release(regc->lock);
     } else {
+	pjsip_cached_auth *auth = NULL;
 	pjsip_tpselector_dec_ref(&regc->tp_sel);
 	if (regc->last_transport) {
@@ -190,4 +191,11 @@
 	pj_lock_destroy(regc->lock);
 	regc->lock = NULL;
+
+	auth = regc->auth_sess.cached_auth.next;
+	while (auth != &regc->auth_sess.cached_auth) {
+	    pjsip_endpt_release_pool(regc->endpt, auth->pool);
+	    auth = auth->next;
+	}
+
 	pjsip_endpt_release_pool(regc->endpt, regc->pool);
     }
Index: pjproject/trunk/pjsip/src/pjsip/sip_auth_client.c
===================================================================
--- pjproject/trunk/pjsip/src/pjsip/sip_auth_client.c	(revision 5335)
+++ pjproject/trunk/pjsip/src/pjsip/sip_auth_client.c	(revision 5336)
@@ -358,6 +358,5 @@
  * Update authentication session with a challenge.
  */
-static void update_digest_session( pj_pool_t *ses_pool,
-				   pjsip_cached_auth *cached_auth,
+static void update_digest_session( pjsip_cached_auth *cached_auth,
 				   const pjsip_www_authenticate_hdr *hdr )
 {
@@ -366,5 +365,5 @@
 	if (!cached_auth->last_chal || pj_stricmp2(&hdr->scheme, "digest")) {
 	    cached_auth->last_chal = (pjsip_www_authenticate_hdr*)
-				     pjsip_hdr_clone(ses_pool, hdr);
+				     pjsip_hdr_clone(cached_auth->pool, hdr);
 	} else {
 	    /* Only update if the new challenge is "significantly different"
@@ -383,5 +382,5 @@
 	    {
 		cached_auth->last_chal = (pjsip_www_authenticate_hdr*)
-				         pjsip_hdr_clone(ses_pool, hdr);
+				       pjsip_hdr_clone(cached_auth->pool, hdr);
 	    }
 	}
@@ -394,8 +393,8 @@
 	/* Save the whole challenge */
 	cached_auth->last_chal = (pjsip_www_authenticate_hdr*)
-				 pjsip_hdr_clone(ses_pool, hdr);
+				 pjsip_hdr_clone(cached_auth->pool, hdr);
 
 	/* Create cnonce */
-	pj_create_unique_string( ses_pool, &cached_auth->cnonce );
+	pj_create_unique_string( cached_auth->pool, &cached_auth->cnonce );
 
 	/* Initialize nonce-count */
@@ -407,5 +406,5 @@
 	*/
 	if (cached_auth->realm.slen == 0) {
-	    pj_strdup(ses_pool, &cached_auth->realm,
+	    pj_strdup(cached_auth->pool, &cached_auth->realm,
 		      &hdr->challenge.digest.realm);
 	}
@@ -420,5 +419,6 @@
 	} else {
 	    /* Server gives new nonce. */
-	    pj_strdup(ses_pool, &cached_auth->last_chal->challenge.digest.nonce,
+	    pj_strdup(cached_auth->pool, 
+		      &cached_auth->last_chal->challenge.digest.nonce,
 		      &hdr->challenge.digest.nonce);
 	    /* Has the opaque changed? */
@@ -426,5 +426,5 @@
 			  &hdr->challenge.digest.opaque))
 	    {
-		pj_strdup(ses_pool,
+		pj_strdup(cached_auth->pool,
 			  &cached_auth->last_chal->challenge.digest.opaque,
 			  &hdr->challenge.digest.opaque);
@@ -692,5 +692,5 @@
 	{
 	    if (cached_auth) {
-		update_digest_session( sess_pool, cached_auth, hdr );
+		update_digest_session( cached_auth, hdr );
 
 		cnonce = &cached_auth->cnonce;
@@ -961,4 +961,31 @@
 }
 
+
+static void recreate_cached_auth_pool( pjsip_endpoint *endpt, 
+				       pjsip_cached_auth *auth )
+{
+    pj_pool_t *auth_pool = pjsip_endpt_create_pool(endpt, "regc_auth%p", 1024, 
+						   1024);
+
+    if (auth->realm.slen) {
+        pj_str_t realm;
+        pj_strdup(auth_pool, &realm, &auth->realm);
+        pj_strassign(&auth->realm, &realm);
+    }
+
+    if (auth->cnonce.slen) {
+        pj_str_t cnonce;
+        pj_strdup(auth_pool, &cnonce, &auth->cnonce);
+        pj_strassign(&auth->cnonce, &cnonce);
+    }
+
+    if (auth->last_chal) {
+        auth->last_chal = (pjsip_www_authenticate_hdr*)
+			  pjsip_hdr_clone(auth_pool, auth->last_chal);
+    }
+
+    pjsip_endpt_release_pool(endpt, auth->pool);
+    auth->pool = auth_pool;
+}
 
 /* Process authorization challenge */
@@ -1127,5 +1154,5 @@
 	    break;
 
-	hchal = (const pjsip_www_authenticate_hdr*) hdr;
+	hchal = (const pjsip_www_authenticate_hdr*)hdr;
 	++chal_cnt;
 
@@ -1133,8 +1160,13 @@
 	 * if not present.
 	 */
-	cached_auth = find_cached_auth(sess, &hchal->challenge.common.realm );
+	cached_auth = find_cached_auth(sess, &hchal->challenge.common.realm);
 	if (!cached_auth) {
-	    cached_auth = PJ_POOL_ZALLOC_T( sess->pool, pjsip_cached_auth);
-	    pj_strdup( sess->pool, &cached_auth->realm, &hchal->challenge.common.realm);
+	    cached_auth = PJ_POOL_ZALLOC_T(sess->pool, pjsip_cached_auth);
+	    cached_auth->pool = pjsip_endpt_create_pool(sess->endpt,
+							"regc_auth%p",
+							1024,
+							1024);
+	    pj_strdup(cached_auth->pool, &cached_auth->realm,
+		      &hchal->challenge.common.realm);
 	    cached_auth->is_proxy = (hchal->type == PJSIP_H_PROXY_AUTHENTICATE);
 #	    if (PJSIP_AUTH_HEADER_CACHING)
@@ -1143,5 +1175,5 @@
 	    }
 #	    endif
-	    pj_list_insert_before( &sess->cached_auth, cached_auth );
+	    pj_list_insert_before(&sess->cached_auth, cached_auth);
 	}
 
@@ -1149,8 +1181,14 @@
 	 * authorization session.
 	 */
-	status = process_auth( tdata->pool, hchal, tdata->msg->line.req.uri,
-			       tdata, sess, cached_auth, &hauth);
+	status = process_auth(tdata->pool, hchal, tdata->msg->line.req.uri,
+			      tdata, sess, cached_auth, &hauth);
 	if (status != PJ_SUCCESS)
 	    return status;
+
+	if (pj_pool_get_used_size(cached_auth->pool) >
+	    PJSIP_AUTH_CACHED_POOL_MAX_SIZE) 
+	{
+	    recreate_cached_auth_pool(sess->endpt, cached_auth);
+	}	
 
 	/* Add to the message. */
