Index: /trunk/src/VBox/Main/include/ConsoleImpl.h
===================================================================
--- /trunk/src/VBox/Main/include/ConsoleImpl.h	(revision 35871)
+++ /trunk/src/VBox/Main/include/ConsoleImpl.h	(revision 35872)
@@ -471,4 +471,5 @@
 
     static DECLCALLBACK(int) configConstructor(PVM pVM, void *pvConsole);
+    int configConstructorInner(PVM pVM, AutoWriteLock *pAlock);
     int configCfgmOverlay(PVM pVM, IVirtualBox *pVirtualBox, IMachine *pMachine);
 
Index: /trunk/src/VBox/Main/src-client/ConsoleImpl2.cpp
===================================================================
--- /trunk/src/VBox/Main/src-client/ConsoleImpl2.cpp	(revision 35871)
+++ /trunk/src/VBox/Main/src-client/ConsoleImpl2.cpp	(revision 35872)
@@ -553,11 +553,29 @@
     AutoWriteLock alock(pConsole COMMA_LOCKVAL_SRC_POS);
 
-    /* Save the VM pointer in the machine object */
+    /*
+     * Set the VM handle and do the rest of the job in an worker method so we
+     * can easily reset the VM handle on failure.
+     */
     pConsole->mpVM = pVM;
-
-    VMMDev *pVMMDev = pConsole->m_pVMMDev;
+    int vrc = pConsole->configConstructorInner(pVM, &alock);
+    if (RT_FAILURE(vrc))
+        pConsole->mpVM = NULL;
+
+    return vrc;
+}
+
+
+/**
+ * Worker for configConstructor.
+ *
+ * @return  VBox status code.
+ * @param   pVM         The VM handle.
+ */
+int Console::configConstructorInner(PVM pVM, AutoWriteLock *pAlock)
+{
+    VMMDev *pVMMDev = m_pVMMDev;
     Assert(pVMMDev);
 
-    ComPtr<IMachine> pMachine = pConsole->machine();
+    ComPtr<IMachine> pMachine = machine();
 
     int             rc;
@@ -609,5 +627,5 @@
     }
 
-    BusAssignmentManager* BusMgr = pConsole->mBusMgr = BusAssignmentManager::createInstance(chipsetType);
+    BusAssignmentManager* BusMgr = mBusMgr = BusAssignmentManager::createInstance(chipsetType);
 
     ULONG cCpus = 1;
@@ -983,5 +1001,5 @@
 #ifdef VBOX_WITH_PCI_PASSTHROUGH
             /* Add PCI passthrough devices */
-            hrc = attachRawPciDevices(BusMgr, pDevices, pConsole);                               H();
+            hrc = attachRawPciDevices(BusMgr, pDevices, this);                                   H();
 #endif
         }
@@ -1060,5 +1078,5 @@
         InsertConfigString(pLunL1, "Driver",               "MainKeyboard");
         InsertConfigNode(pLunL1,   "Config", &pCfg);
-        Keyboard *pKeyboard = pConsole->mKeyboard;
+        Keyboard *pKeyboard = mKeyboard;
         InsertConfigInteger(pCfg,  "Object",     (uintptr_t)pKeyboard);
 
@@ -1071,5 +1089,5 @@
         InsertConfigString(pLunL1, "Driver",               "MainMouse");
         InsertConfigNode(pLunL1,   "Config", &pCfg);
-        Mouse *pMouse = pConsole->mMouse;
+        Mouse *pMouse = mMouse;
         InsertConfigInteger(pCfg,  "Object",     (uintptr_t)pMouse);
 
@@ -1190,5 +1208,5 @@
         /* VESA height reduction */
         ULONG ulHeightReduction;
-        IFramebuffer *pFramebuffer = pConsole->getDisplay()->getFramebuffer();
+        IFramebuffer *pFramebuffer = getDisplay()->getFramebuffer();
         if (pFramebuffer)
         {
@@ -1206,5 +1224,5 @@
         InsertConfigString(pLunL0, "Driver",               "MainDisplay");
         InsertConfigNode(pLunL0,   "Config", &pCfg);
-        Display *pDisplay = pConsole->mDisplay;
+        Display *pDisplay = mDisplay;
         InsertConfigInteger(pCfg,  "Object", (uintptr_t)pDisplay);
 
@@ -1383,5 +1401,5 @@
 
             /* /Devices/<ctrldev>/ */
-            const char *pszCtrlDev = pConsole->convertControllerTypeToDev(enmCtrlType);
+            const char *pszCtrlDev = convertControllerTypeToDev(enmCtrlType);
             pDev = aCtrlNodes[enmCtrlType];
             if (!pDev)
@@ -1411,9 +1429,9 @@
                     InsertConfigString(pLunL0, "Driver",               "MainStatus");
                     InsertConfigNode(pLunL0,   "Config", &pCfg);
-                    InsertConfigInteger(pCfg,  "papLeds", (uintptr_t)&pConsole->mapStorageLeds[iLedScsi]);
+                    InsertConfigInteger(pCfg,  "papLeds", (uintptr_t)&mapStorageLeds[iLedScsi]);
                     InsertConfigInteger(pCfg,  "First",    0);
                     Assert(cLedScsi >= 16);
                     InsertConfigInteger(pCfg,  "Last",     15);
-                    paLedDevType = &pConsole->maStorageDevType[iLedScsi];
+                    paLedDevType = &maStorageDevType[iLedScsi];
                     break;
                 }
@@ -1429,9 +1447,9 @@
                     InsertConfigString(pLunL0, "Driver",               "MainStatus");
                     InsertConfigNode(pLunL0,   "Config", &pCfg);
-                    InsertConfigInteger(pCfg,  "papLeds", (uintptr_t)&pConsole->mapStorageLeds[iLedScsi]);
+                    InsertConfigInteger(pCfg,  "papLeds", (uintptr_t)&mapStorageLeds[iLedScsi]);
                     InsertConfigInteger(pCfg,  "First",    0);
                     Assert(cLedScsi >= 16);
                     InsertConfigInteger(pCfg,  "Last",     15);
-                    paLedDevType = &pConsole->maStorageDevType[iLedScsi];
+                    paLedDevType = &maStorageDevType[iLedScsi];
                     break;
                 }
@@ -1474,8 +1492,8 @@
                     InsertConfigNode(pLunL0,   "Config", &pCfg);
                     AssertRelease(cPorts <= cLedSata);
-                    InsertConfigInteger(pCfg,  "papLeds", (uintptr_t)&pConsole->mapStorageLeds[iLedSata]);
+                    InsertConfigInteger(pCfg,  "papLeds", (uintptr_t)&mapStorageLeds[iLedSata]);
                     InsertConfigInteger(pCfg,  "First",    0);
                     InsertConfigInteger(pCfg,  "Last",     cPorts - 1);
-                    paLedDevType = &pConsole->maStorageDevType[iLedSata];
+                    paLedDevType = &maStorageDevType[iLedSata];
                     break;
                 }
@@ -1495,9 +1513,9 @@
                     InsertConfigString(pLunL0, "Driver",               "MainStatus");
                     InsertConfigNode(pLunL0,   "Config", &pCfg);
-                    InsertConfigInteger(pCfg,  "papLeds", (uintptr_t)&pConsole->mapStorageLeds[iLedIde]);
+                    InsertConfigInteger(pCfg,  "papLeds", (uintptr_t)&mapStorageLeds[iLedIde]);
                     InsertConfigInteger(pCfg,  "First",    0);
                     Assert(cLedIde >= 4);
                     InsertConfigInteger(pCfg,  "Last",     3);
-                    paLedDevType = &pConsole->maStorageDevType[iLedIde];
+                    paLedDevType = &maStorageDevType[iLedIde];
 
                     /* IDE flavors */
@@ -1523,9 +1541,9 @@
                     InsertConfigString(pLunL0, "Driver",               "MainStatus");
                     InsertConfigNode(pLunL0,   "Config", &pCfg);
-                    InsertConfigInteger(pCfg,  "papLeds", (uintptr_t)&pConsole->mapStorageLeds[iLedFloppy]);
+                    InsertConfigInteger(pCfg,  "papLeds", (uintptr_t)&mapStorageLeds[iLedFloppy]);
                     InsertConfigInteger(pCfg,  "First",    0);
                     Assert(cLedFloppy >= 1);
                     InsertConfigInteger(pCfg,  "Last",     0);
-                    paLedDevType = &pConsole->maStorageDevType[iLedFloppy];
+                    paLedDevType = &maStorageDevType[iLedFloppy];
                     break;
                 }
@@ -1542,9 +1560,9 @@
                     InsertConfigString(pLunL0, "Driver",               "MainStatus");
                     InsertConfigNode(pLunL0,   "Config", &pCfg);
-                    InsertConfigInteger(pCfg,  "papLeds", (uintptr_t)&pConsole->mapStorageLeds[iLedSas]);
+                    InsertConfigInteger(pCfg,  "papLeds", (uintptr_t)&mapStorageLeds[iLedSas]);
                     InsertConfigInteger(pCfg,  "First",    0);
                     Assert(cLedSas >= 8);
                     InsertConfigInteger(pCfg,  "Last",     7);
-                    paLedDevType = &pConsole->maStorageDevType[iLedSas];
+                    paLedDevType = &maStorageDevType[iLedSas];
                     break;
                 }
@@ -1566,20 +1584,20 @@
             for (size_t j = 0; j < atts.size(); ++j)
             {
-                rc = pConsole->configMediumAttachment(pCtlInst,
-                                                      pszCtrlDev,
-                                                      ulInstance,
-                                                      enmBus,
-                                                      !!fUseHostIOCache,
-                                                      !!fBuiltinIoCache,
-                                                      false /* fSetupMerge */,
-                                                      0 /* uMergeSource */,
-                                                      0 /* uMergeTarget */,
-                                                      atts[j],
-                                                      pConsole->mMachineState,
-                                                      NULL /* phrc */,
-                                                      false /* fAttachDetach */,
-                                                      false /* fForceUnmount */,
-                                                      pVM,
-                                                      paLedDevType);
+                rc = configMediumAttachment(pCtlInst,
+                                            pszCtrlDev,
+                                            ulInstance,
+                                            enmBus,
+                                            !!fUseHostIOCache,
+                                            !!fBuiltinIoCache,
+                                            false /* fSetupMerge */,
+                                            0 /* uMergeSource */,
+                                            0 /* uMergeTarget */,
+                                            atts[j],
+                                            mMachineState,
+                                            NULL /* phrc */,
+                                            false /* fAttachDetach */,
+                                            false /* fForceUnmount */,
+                                            pVM,
+                                            paLedDevType);
                 if (RT_FAILURE(rc))
                     return rc;
@@ -1772,19 +1790,19 @@
             InsertConfigString(pLunL0, "Driver",               "MainStatus");
             InsertConfigNode(pLunL0,   "Config", &pCfg);
-            InsertConfigInteger(pCfg,  "papLeds", (uintptr_t)&pConsole->mapNetworkLeds[ulInstance]);
+            InsertConfigInteger(pCfg,  "papLeds", (uintptr_t)&mapNetworkLeds[ulInstance]);
 
             /*
              * Configure the network card now
              */
-            bool fIgnoreConnectFailure = pConsole->mMachineState == MachineState_Restoring;
-            rc = pConsole->configNetwork(pszAdapterName,
-                                         ulInstance,
-                                         0,
-                                         networkAdapter,
-                                         pCfg,
-                                         pLunL0,
-                                         pInst,
-                                         false /*fAttachDetach*/,
-                                         fIgnoreConnectFailure);
+            bool fIgnoreConnectFailure = mMachineState == MachineState_Restoring;
+            rc = configNetwork(pszAdapterName,
+                               ulInstance,
+                               0,
+                               networkAdapter,
+                               pCfg,
+                               pLunL0,
+                               pInst,
+                               false /*fAttachDetach*/,
+                               fIgnoreConnectFailure);
             if (RT_FAILURE(rc))
                 return rc;
@@ -1943,5 +1961,5 @@
         InsertConfigString(pLunL0, "Driver",               "MainStatus");
         InsertConfigNode(pLunL0,   "Config", &pCfg);
-        InsertConfigInteger(pCfg,  "papLeds", (uintptr_t)&pConsole->mapSharedFolderLed);
+        InsertConfigInteger(pCfg,  "papLeds", (uintptr_t)&mapSharedFolderLed);
         InsertConfigInteger(pCfg,  "First",    0);
         InsertConfigInteger(pCfg,  "Last",     0);
@@ -1958,5 +1976,5 @@
         InsertConfigString(pLunL0, "Driver",               "MainAudioSniffer");
         InsertConfigNode(pLunL0,   "Config", &pCfg);
-        AudioSniffer *pAudioSniffer = pConsole->mAudioSniffer;
+        AudioSniffer *pAudioSniffer = mAudioSniffer;
         InsertConfigInteger(pCfg,  "Object", (uintptr_t)pAudioSniffer);
 
@@ -2116,5 +2134,5 @@
                 InsertConfigString(pLunL0, "Driver",               "MainStatus");
                 InsertConfigNode(pLunL0,   "Config", &pCfg);
-                InsertConfigInteger(pCfg,  "papLeds", (uintptr_t)&pConsole->mapUSBLed[0]);
+                InsertConfigInteger(pCfg,  "papLeds", (uintptr_t)&mapUSBLed[0]);
                 InsertConfigInteger(pCfg,  "First",    0);
                 InsertConfigInteger(pCfg,  "Last",     0);
@@ -2135,5 +2153,5 @@
                     static const char *s_pszUsbExtPackName = "Oracle VM VirtualBox Extension Pack";
 # ifdef VBOX_WITH_EXTPACK
-                    if (pConsole->mptrExtPackManager->isExtPackUsable(s_pszUsbExtPackName))
+                    if (mptrExtPackManager->isExtPackUsable(s_pszUsbExtPackName))
 # endif
                     {
@@ -2154,5 +2172,5 @@
                         InsertConfigString(pLunL0, "Driver",               "MainStatus");
                         InsertConfigNode(pLunL0,   "Config", &pCfg);
-                        InsertConfigInteger(pCfg,  "papLeds", (uintptr_t)&pConsole->mapUSBLed[1]);
+                        InsertConfigInteger(pCfg,  "papLeds", (uintptr_t)&mapUSBLed[1]);
                         InsertConfigInteger(pCfg,  "First",    0);
                         InsertConfigInteger(pCfg,  "Last",     0);
@@ -2162,5 +2180,5 @@
                     {
                         /* Fatal if a saved state is being restored, otherwise ignorable. */
-                        if (pConsole->mMachineState == MachineState_Restoring)
+                        if (mMachineState == MachineState_Restoring)
                             return VMSetError(pVM, VERR_NOT_FOUND, RT_SRC_POS,
                                     N_("Implementation of the USB 2.0 controller not found!\n"
@@ -2170,5 +2188,5 @@
                                        "support in the VM settings"),
                                     s_pszUsbExtPackName);
-                        setVMRuntimeErrorCallbackF(pVM, pConsole, 0, "ExtPackNoEhci",
+                        setVMRuntimeErrorCallbackF(pVM, this, 0, "ExtPackNoEhci",
                                 N_("Implementation of the USB 2.0 controller not found!\n"
                                    "The device will be disabled. You can ignore this warning "
@@ -2253,5 +2271,5 @@
                     InsertConfigString(pLunL1, "Driver",        "MainMouse");
                     InsertConfigNode(pLunL1,   "Config", &pCfg);
-                    pMouse = pConsole->mMouse;
+                    pMouse = mMouse;
                     InsertConfigInteger(pCfg,  "Object",     (uintptr_t)pMouse);
                 }
@@ -2274,5 +2292,5 @@
                     InsertConfigString(pLunL1, "Driver",               "MainKeyboard");
                     InsertConfigNode(pLunL1,   "Config", &pCfg);
-                    pKeyboard = pConsole->mKeyboard;
+                    pKeyboard = mKeyboard;
                     InsertConfigInteger(pCfg,  "Object",     (uintptr_t)pKeyboard);
                 }
@@ -2367,5 +2385,5 @@
                     parm.type = VBOX_HGCM_SVC_PARM_PTR;
 
-                    parm.u.pointer.addr = (IConsole*) (Console*) pConsole;
+                    parm.u.pointer.addr = (IConsole *)(Console *)this;
                     parm.u.pointer.size = sizeof(IConsole *);
 
@@ -2390,5 +2408,5 @@
          */
 
-        rc = configGuestProperties(pConsole);
+        rc = configGuestProperties(this);
 #endif /* VBOX_WITH_GUEST_PROPS defined */
 
@@ -2398,5 +2416,5 @@
          */
 
-        rc = configGuestControl(pConsole);
+        rc = configGuestControl(this);
 #endif /* VBOX_WITH_GUEST_CONTROL defined */
 
@@ -2500,7 +2518,7 @@
     if (RT_SUCCESS(rc))
     {
-        alock.release();
-        rc = pConsole->mptrExtPackManager->callAllVmConfigureVmmHooks(pConsole, pVM);
-        alock.acquire();
+        pAlock->release();
+        rc = mptrExtPackManager->callAllVmConfigureVmmHooks(this, pVM);
+        pAlock->acquire();
     }
 #endif
@@ -2510,5 +2528,5 @@
      */
     if (RT_SUCCESS(rc))
-        rc = pConsole->configCfgmOverlay(pVM, virtualBox, pMachine);
+        rc = configCfgmOverlay(pVM, virtualBox, pMachine);
 
 #undef H
@@ -2517,5 +2535,5 @@
      * Register VM state change handler.
      */
-    int rc2 = VMR3AtStateRegister(pVM, Console::vmstateChangeCallback, pConsole);
+    int rc2 = VMR3AtStateRegister(pVM, Console::vmstateChangeCallback, this);
     AssertRC(rc2);
     if (RT_SUCCESS(rc))
@@ -2525,5 +2543,5 @@
      * Register VM runtime error handler.
      */
-    rc2 = VMR3AtRuntimeErrorRegister(pVM, Console::setVMRuntimeErrorCallback, pConsole);
+    rc2 = VMR3AtRuntimeErrorRegister(pVM, Console::setVMRuntimeErrorCallback, this);
     AssertRC(rc2);
     if (RT_SUCCESS(rc))
