Related to ticket #192: crash when shutting down pjsua if buddy pool has been destroyed when presence subscription callback is called

git-svn-id: https://svn.pjsip.org/repos/pjproject/trunk@2157 74dad513-b988-da41-8d7b-12977e46ad98
diff --git a/pjsip/src/pjsua-lib/pjsua_core.c b/pjsip/src/pjsua-lib/pjsua_core.c
index dbc917b..bd21a4b 100644
--- a/pjsip/src/pjsua-lib/pjsua_core.c
+++ b/pjsip/src/pjsua-lib/pjsua_core.c
@@ -1093,6 +1093,14 @@
 
 	PJ_LOG(4,(THIS_FILE, "Destroying..."));
 
+	/* Must destroy endpoint first before destroying pools in
+	 * buddies or accounts, since shutting down transaction layer
+	 * may emit events which trigger some buddy or account callbacks
+	 * to be called.
+	 */
+	pjsip_endpt_destroy(pjsua_var.endpt);
+	pjsua_var.endpt = NULL;
+
 	/* Destroy pool in the buddy object */
 	for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
 	    if (pjsua_var.buddy[i].pool) {
@@ -1108,9 +1116,6 @@
 		pjsua_var.acc[i].pool = NULL;
 	    }
 	}
-
-	pjsip_endpt_destroy(pjsua_var.endpt);
-	pjsua_var.endpt = NULL;
     }
 
     /* Destroy mutex */