Index: /pjproject/trunk/pjsip/include/pjsua2/account.hpp
===================================================================
--- /pjproject/trunk/pjsip/include/pjsua2/account.hpp	(revision 5800)
+++ /pjproject/trunk/pjsip/include/pjsua2/account.hpp	(revision 5801)
@@ -1510,4 +1510,10 @@
      * Destructor. Note that if the account is deleted, it will also delete
      * the corresponding account in the PJSUA-LIB.
+     *
+     * If application implements a derived class, the derived class should
+     * call shutdown() in the beginning stage in its destructor, or
+     * alternatively application should call shutdown() before deleting
+     * the derived class instance. This is to avoid race condition between
+     * the derived class destructor and Account callbacks.
      */
     virtual ~Account();
@@ -1515,4 +1521,10 @@
     /**
      * Create the account.
+     *
+     * If application implements a derived class, the derived class should
+     * call shutdown() in the beginning stage in its destructor, or
+     * alternatively application should call shutdown() before deleting
+     * the derived class instance. This is to avoid race condition between
+     * the derived class destructor and Account callbacks.
      *
      * @param cfg		The account config.
@@ -1521,4 +1533,16 @@
     void create(const AccountConfig &cfg,
                 bool make_default=false) throw(Error);
+
+    /**
+     * Shutdown the account. This will initiate unregistration if needed,
+     * and delete the corresponding account in the PJSUA-LIB.
+     *
+     * If application implements a derived class, the derived class should
+     * call this method in the beginning stage in its destructor, or
+     * alternatively application should call this method before deleting
+     * the derived class instance. This is to avoid race condition between
+     * the derived class destructor and Account callbacks.
+     */
+    void shutdown();
 
     /**
@@ -1542,6 +1566,4 @@
      * Set this as default account to be used when incoming and outgoing
      * requests don't match any accounts.
-     *
-     * @return			PJ_SUCCESS on success.
      */
     void setDefault() throw(Error);
@@ -1645,16 +1667,4 @@
     Buddy* findBuddy(string uri, FindBuddyMatch *buddy_match = NULL) const
 		    throw(Error);
-
-    /**
-     * An internal function to add a Buddy to Account buddy list.
-     * This function must never be used by application.
-     */
-    void addBuddy(Buddy *buddy);
-
-    /**
-     * An internal function to remove a Buddy from Account buddy list.
-     * This function must never be used by application.
-     */
-    void removeBuddy(Buddy *buddy);
 
 public:
@@ -1763,6 +1773,21 @@
     { PJ_UNUSED_ARG(prm); }
 
-protected:
+
+private:
     friend class Endpoint;
+    friend class Buddy;
+
+    /**
+     * An internal function to add a Buddy to Account buddy list.
+     * This method is used by Buddy::create().
+     */
+    void addBuddy(Buddy *buddy);
+
+    /**
+     * An internal function to remove a Buddy from Account buddy list.
+     * This method is used by Buddy::~Buddy().
+     */
+    void removeBuddy(Buddy *buddy);
+
 
 private:
Index: /pjproject/trunk/pjsip/src/pjsua2/account.cpp
===================================================================
--- /pjproject/trunk/pjsip/src/pjsua2/account.cpp	(revision 5800)
+++ /pjproject/trunk/pjsip/src/pjsua2/account.cpp	(revision 5801)
@@ -820,4 +820,19 @@
      * PJSUA library.
      */
+    shutdown();
+}
+
+void Account::create(const AccountConfig &acc_cfg,
+                     bool make_default) throw(Error)
+{
+    pjsua_acc_config pj_acc_cfg;
+    
+    acc_cfg.toPj(pj_acc_cfg);
+    pj_acc_cfg.user_data = (void*)this;
+    PJSUA2_CHECK_EXPR( pjsua_acc_add(&pj_acc_cfg, make_default, &id) );
+}
+
+void Account::shutdown()
+{
     if (isValid() && pjsua_get_state() < PJSUA_STATE_CLOSING) {
         // Cleanup buddies in the buddy list
@@ -835,14 +850,4 @@
 }
 
-void Account::create(const AccountConfig &acc_cfg,
-                     bool make_default) throw(Error)
-{
-    pjsua_acc_config pj_acc_cfg;
-    
-    acc_cfg.toPj(pj_acc_cfg);
-    pj_acc_cfg.user_data = (void*)this;
-    PJSUA2_CHECK_EXPR( pjsua_acc_add(&pj_acc_cfg, make_default, &id) );
-}
-
 void Account::modify(const AccountConfig &acc_cfg) throw(Error)
 {
