Index: pjproject/trunk/pjlib/include/pj/errno.h
===================================================================
--- pjproject/trunk/pjlib/include/pj/errno.h	(revision 838)
+++ pjproject/trunk/pjlib/include/pj/errno.h	(revision 839)
@@ -347,4 +347,7 @@
 PJ_END_DECL
 
+/* Internal */
+void pj_errno_clear_handlers(void);
+
 #endif	/* __PJ_ERRNO_H__ */
 
Index: pjproject/trunk/pjlib/src/pj/config.c
===================================================================
--- pjproject/trunk/pjlib/src/pj/config.c	(revision 838)
+++ pjproject/trunk/pjlib/src/pj/config.c	(revision 839)
@@ -22,5 +22,5 @@
 
 static const char *id = "config.c";
-const char *PJ_VERSION = "0.5.9";
+const char *PJ_VERSION = "0.5.9.1";
 
 PJ_DEF(void) pj_dump_config(void)
Index: pjproject/trunk/pjlib/src/pj/errno.c
===================================================================
--- pjproject/trunk/pjlib/src/pj/errno.c	(revision 838)
+++ pjproject/trunk/pjlib/src/pj/errno.c	(revision 839)
@@ -134,4 +134,12 @@
 }
 
+/* Internal PJLIB function called by pj_shutdown() to clear error handlers */
+void pj_errno_clear_handlers(void)
+{
+    err_msg_hnd_cnt = 0;
+    pj_bzero(err_msg_hnd, sizeof(err_msg_hnd));
+}
+
+
 /*
  * pj_strerror()
Index: pjproject/trunk/pjlib/src/pj/except.c
===================================================================
--- pjproject/trunk/pjlib/src/pj/except.c	(revision 838)
+++ pjproject/trunk/pjlib/src/pj/except.c	(revision 839)
@@ -58,4 +58,14 @@
 	thread_local_id = -1;
     }
+
+#if defined(PJ_HAS_EXCEPTION_NAMES) && PJ_HAS_EXCEPTION_NAMES != 0
+    {
+	unsigned i;
+	for (i=0; i<PJ_MAX_EXCEPTION_ID; ++i)
+	    exception_id_names[i] = NULL;
+    }
+#else
+    last_exception_id = 1;
+#endif
 }
 
Index: pjproject/trunk/pjlib/src/pj/os_core_unix.c
===================================================================
--- pjproject/trunk/pjlib/src/pj/os_core_unix.c	(revision 838)
+++ pjproject/trunk/pjlib/src/pj/os_core_unix.c	(revision 839)
@@ -208,4 +208,7 @@
     }
 #endif
+
+    /* Clear static variables */
+    pj_errno_clear_handlers();
 }
 
Index: pjproject/trunk/pjlib/src/pj/os_core_win32.c
===================================================================
--- pjproject/trunk/pjlib/src/pj/os_core_win32.c	(revision 838)
+++ pjproject/trunk/pjlib/src/pj/os_core_win32.c	(revision 839)
@@ -219,4 +219,7 @@
     }
 
+    /* Clear static variables */
+    pj_errno_clear_handlers();
+
     /* Shutdown Winsock */
     //WSACleanup();
Index: pjproject/trunk/pjlib/src/pj/pool_buf.c
===================================================================
--- pjproject/trunk/pjlib/src/pj/pool_buf.c	(revision 838)
+++ pjproject/trunk/pjlib/src/pj/pool_buf.c	(revision 839)
@@ -39,4 +39,6 @@
 	tls = -1;
     }
+    if (is_initialized)
+	is_initialized = 0;
 }
 
Index: pjproject/trunk/pjsip-apps/src/pjsua/pjsua_app.c
===================================================================
--- pjproject/trunk/pjsip-apps/src/pjsua/pjsua_app.c	(revision 838)
+++ pjproject/trunk/pjsip-apps/src/pjsua/pjsua_app.c	(revision 839)
@@ -362,4 +362,5 @@
 
     /* Run pj_getopt once to see if user specifies config file to read. */ 
+    pj_optind = 0;
     while ((c=pj_getopt_long(argc, argv, "", long_options, 
 			     &option_index)) != -1) 
@@ -2846,4 +2847,6 @@
 pj_status_t app_destroy(void)
 {
+    pj_status_t status;
+
 #ifdef STEREO_DEMO
     if (app_config.snd) {
@@ -2858,5 +2861,9 @@
     }
 
-    return pjsua_destroy();
+    status = pjsua_destroy();
+
+    pj_bzero(&app_config, sizeof(app_config));
+
+    return status;
 }
 
Index: pjproject/trunk/pjsip/src/pjsip/sip_endpoint.c
===================================================================
--- pjproject/trunk/pjsip/src/pjsip/sip_endpoint.c	(revision 838)
+++ pjproject/trunk/pjsip/src/pjsip/sip_endpoint.c	(revision 839)
@@ -109,4 +109,5 @@
 /* Defined in sip_parser.c */
 void init_sip_parser(void);
+void deinit_sip_parser(void);
 
 /* Defined in sip_tel_uri.c */
@@ -588,4 +589,7 @@
     /* Delete endpoint mutex. */
     pj_mutex_destroy(endpt->mutex);
+
+    /* Deinit parser */
+    deinit_sip_parser();
 
     /* Delete module's mutex */
Index: pjproject/trunk/pjsip/src/pjsip/sip_parser.c
===================================================================
--- pjproject/trunk/pjsip/src/pjsip/sip_parser.c	(revision 838)
+++ pjproject/trunk/pjsip/src/pjsip/sip_parser.c	(revision 839)
@@ -282,11 +282,5 @@
 static pj_status_t init_parser()
 {
-    static int initialized;
     pj_status_t status;
-
-    if (initialized)
-	return PJ_SUCCESS;
-
-    initialized = 1;
 
     /*
@@ -473,10 +467,24 @@
 void init_sip_parser(void)
 {
-    if (!parser_is_initialized) {
-	if (!parser_is_initialized) {
-	    init_parser();
-	    parser_is_initialized = 1;
-	}
-    }
+    pj_enter_critical_section();
+    if (++parser_is_initialized == 1) {
+	init_parser();
+    }
+    pj_leave_critical_section();
+}
+
+void deinit_sip_parser(void)
+{
+    pj_enter_critical_section();
+    if (--parser_is_initialized == 0) {
+	/* Clear header handlers */
+	pj_bzero(handler, sizeof(handler));
+	handler_count = 0;
+
+	/* Clear URI handlers */
+	pj_bzero(uri_handler, sizeof(uri_handler));
+	uri_handler_count = 0;
+    }
+    pj_leave_critical_section();
 }
 
Index: pjproject/trunk/pjsip/src/pjsip/sip_transaction.c
===================================================================
--- pjproject/trunk/pjsip/src/pjsip/sip_transaction.c	(revision 838)
+++ pjproject/trunk/pjsip/src/pjsip/sip_transaction.c	(revision 839)
@@ -176,5 +176,5 @@
 static pj_status_t tsx_create( pjsip_module *tsx_user,
 			       pjsip_transaction **p_tsx);
-static void	   tsx_destroy( pjsip_transaction *tsx );
+static pj_status_t tsx_destroy( pjsip_transaction *tsx );
 static void	   tsx_resched_retransmission( pjsip_transaction *tsx );
 static pj_status_t tsx_retransmit( pjsip_transaction *tsx, int resched);
@@ -645,6 +645,8 @@
 	pjsip_transaction *tsx = pj_hash_this(mod_tsx_layer.htable, it);
 	pj_hash_iterator_t *next = pj_hash_next(mod_tsx_layer.htable, it);
-	if (tsx)
+	if (tsx) {
+	    mod_tsx_layer_unregister_tsx(tsx);
 	    tsx_destroy(tsx);
+	}
 	it = next;
     }
@@ -921,5 +923,5 @@
 
 /* Destroy transaction. */
-static void tsx_destroy( pjsip_transaction *tsx )
+static pj_status_t tsx_destroy( pjsip_transaction *tsx )
 {
     struct tsx_lock_data *lck;
@@ -955,5 +957,5 @@
 	PJ_LOG(4,(tsx->obj_name, "Will destroy later because transport is "
 				 "in progress"));
-	return;
+	return PJ_EBUSY;
     }
 
@@ -972,4 +974,6 @@
 
     pjsip_endpt_release_pool(tsx->endpt, tsx->pool);
+
+    return PJ_SUCCESS;
 }
 
Index: pjproject/trunk/pjsip/src/pjsua-lib/pjsua_core.c
===================================================================
--- pjproject/trunk/pjsip/src/pjsua-lib/pjsua_core.c	(revision 838)
+++ pjproject/trunk/pjsip/src/pjsua-lib/pjsua_core.c	(revision 839)
@@ -712,4 +712,7 @@
     pj_shutdown();
 
+    /* Clear pjsua_var */
+    pj_bzero(&pjsua_var, sizeof(pjsua_var));
+
     /* Done. */
     return PJ_SUCCESS;
