Index: /trunk/src/VBox/HostServices/GuestControl/service.cpp
===================================================================
--- /trunk/src/VBox/HostServices/GuestControl/service.cpp	(revision 75873)
+++ /trunk/src/VBox/HostServices/GuestControl/service.cpp	(revision 75874)
@@ -859,4 +859,6 @@
     /** Map containing all connected clients, key is HGCM client ID. */
     ClientStateMap  mClientStateMap; /**< @todo Use VBOXHGCMSVCFNTABLE::cbClient for this! */
+    /** The current master client, NULL if none. */
+    ClientState    *m_pMasterClient;
     /** The master HGCM client ID, UINT32_MAX if none. */
     uint32_t        m_idMasterClient;
@@ -873,4 +875,5 @@
         , mpfnHostCallback(NULL)
         , mpvHostData(NULL)
+        , m_pMasterClient(NULL)
         , m_idMasterClient(UINT32_MAX)
         , m_fLegacyMode(true)
@@ -953,12 +956,12 @@
      */
     ClientState *pClient;
-    try
+    //try - can't currently throw anything.
     {
         pClient = new (pvClient) ClientState(pThis->mpHelpers, idClient);
     }
-    catch (std::bad_alloc &)
-    {
-        return VERR_NO_MEMORY;
-    }
+    //catch (std::bad_alloc &)
+    //{
+    //    return VERR_NO_MEMORY;
+    //}
     try
     {
@@ -976,5 +979,5 @@
      * one through the door.
      */
-/** @todo make picking the master more dynamic/flexible. */
+/** @todo make picking the master more dynamic/flexible? */
     if (   pThis->m_fLegacyMode
         && pThis->m_idMasterClient == UINT32_MAX)
@@ -984,4 +987,5 @@
         {
             LogFunc(("Picking %u as master for now.\n", idClient));
+            pThis->m_pMasterClient  = pClient;
             pThis->m_idMasterClient = idClient;
             pClient->m_fIsMaster = true;
@@ -1039,5 +1043,7 @@
     if (idClient == pThis->m_idMasterClient)
     {
+        pThis->m_pMasterClient  = NULL;
         pThis->m_idMasterClient = UINT32_MAX;
+
         GstCtrlPreparedSession *pCur, *pNext;
         RTListForEachSafe(&pThis->m_PreparedSessions, pCur, pNext, GstCtrlPreparedSession, ListEntry)
@@ -1048,4 +1054,6 @@
         pThis->m_cPreparedSessions = 0;
     }
+    else
+        Assert(pClient != pThis->m_pMasterClient);
 
     if (pThis->mClientStateMap.empty())
@@ -1119,4 +1127,5 @@
     if (RT_SUCCESS(rc))
     {
+        m_pMasterClient  = pClient;
         m_idMasterClient = pClient->m_idClient;
         m_fLegacyMode    = false;
@@ -1551,4 +1560,5 @@
     ASSERT_GUEST_RETURN(!m_fLegacyMode, VERR_ACCESS_DENIED);
     Assert(m_idMasterClient == pClient->m_idClient);
+    Assert(m_pMasterClient == pClient);
 
     /* Now that we know it's the master, we can check for session ID duplicates. */
@@ -1614,4 +1624,5 @@
     ASSERT_GUEST_RETURN(!m_fLegacyMode, VERR_ACCESS_DENIED);
     Assert(m_idMasterClient == pClient->m_idClient);
+    Assert(m_pMasterClient == pClient);
 
     /*
@@ -1685,4 +1696,5 @@
     ASSERT_GUEST_RETURN(!m_fLegacyMode, VERR_ACCESS_DENIED);
     Assert(m_idMasterClient != pClient->m_idClient);
+    Assert(m_pMasterClient != pClient);
     ASSERT_GUEST_RETURN(pClient->m_idSession == UINT32_MAX, VERR_RESOURCE_BUSY);
 
@@ -2149,13 +2161,12 @@
             {
                 Assert(pHostCmd);
-                ClientStateMapIter It = mClientStateMap.find(m_idMasterClient);
-                if (It != mClientStateMap.end())
+                if (m_pMasterClient)
                 {
-                    ClientState *pClient = It->second;
-                    RTListAppend(&pClient->m_HostCmdList, &pHostCmd->m_ListEntry);
+                    RTListAppend(&m_pMasterClient->m_HostCmdList, &pHostCmd->m_ListEntry);
                     pHostCmd = NULL;
 
-                    int rc2 = pClient->Wakeup();
-                    LogFlowFunc(("Woke up client ID=%RU32 (master) -> rc=%Rrc\n", pClient->m_idClient, rc2));
+                    int rc2 = m_pMasterClient->Wakeup();
+                    LogFlowFunc(("Woke up client ID=%RU32 (master) -> rc=%Rrc\n", m_pMasterClient->m_idClient, rc2));
+                    NOREF(rc2);
                 }
                 else
@@ -2206,4 +2217,9 @@
     AssertPtrReturn(pThis, VERR_INVALID_POINTER);
 
+    /* Note! We don't need to save the idSession here because it's only used
+             for sessions and the sessions are not persistent across a state
+             save/restore.  The Main objects aren't there.  Clients shuts down.
+             Only the root service survives, so remember who that is and its mode. */
+
     SSMR3PutU32(pSSM, 1);
     SSMR3PutBool(pSSM, pThis->m_fLegacyMode);
@@ -2218,7 +2234,9 @@
 GstCtrlService::svcLoadState(void *pvService, uint32_t idClient, void *pvClient, PSSMHANDLE pSSM, uint32_t uVersion)
 {
-    RT_NOREF(pvClient);
     SELF *pThis = reinterpret_cast<SELF *>(pvService);
     AssertPtrReturn(pThis, VERR_INVALID_POINTER);
+    ClientState *pClient = reinterpret_cast<ClientState *>(pvClient);
+    AssertReturn(pClient, VERR_INVALID_CLIENT_ID);
+    Assert(pClient->m_idClient == idClient);
 
     if (uVersion >= HGCM_SAVED_STATE_VERSION)
@@ -2238,11 +2256,11 @@
         rc = SSMR3GetBool(pSSM, &fIsMaster);
         AssertRCReturn(rc, rc);
+
+        pClient->m_fIsMaster = fIsMaster;
         if (fIsMaster)
+        {
+            pThis->m_pMasterClient  = pClient;
             pThis->m_idMasterClient = idClient;
-        ClientStateMapIter It = pThis->mClientStateMap.find(idClient);
-        if (It != pThis->mClientStateMap.end())
-            It->second->m_fIsMaster = fIsMaster;
-        else
-            AssertFailed();
+        }
     }
     else
