Index: trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMultiScreenLayout.cpp
===================================================================
--- trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMultiScreenLayout.cpp	(revision 44836)
+++ trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMultiScreenLayout.cpp	(revision 44837)
@@ -75,14 +75,24 @@
     for (int iGuestScreen = 0; iGuestScreen < m_cGuestScreens; ++iGuestScreen)
     {
-        /* If the user ever selected a combination in the view menu, we have the following entry: */
-        QString strTest = machine.GetExtraData(QString("%1%2").arg(GUI_VirtualScreenToHostScreen).arg(iGuestScreen));
-        bool fOk;
-        int iHostScreen = strTest.toInt(&fOk);
-        /* Check if valid: */
-        if (!(   fOk /* Valid data */
-              && iHostScreen >= 0 && iHostScreen < m_cHostScreens /* In the host screen bounds? */
-              && m_screenMap.key(iHostScreen, -1) == -1)) /* Not taken already? */
-        {
-            /* If not, check the position of the guest window in normal mode.
+        /* Initialize variables: */
+        bool fValid = false;
+        int iHostScreen = -1;
+
+        if (!fValid)
+        {
+            /* If the user ever selected a combination in the view menu, we have the following entry: */
+            QString strTest = machine.GetExtraData(QString("%1%2").arg(GUI_VirtualScreenToHostScreen).arg(iGuestScreen));
+            bool fOk;
+            /* Check is this value can be converted: */
+            iHostScreen = strTest.toInt(&fOk);
+            /* Revalidate: */
+            fValid =    fOk /* Valid data */
+                     && iHostScreen >= 0 && iHostScreen < m_cHostScreens /* In the host screen bounds? */
+                     && m_screenMap.key(iHostScreen, -1) == -1; /* Not taken already? */
+        }
+
+        if (!fValid)
+        {
+            /* Check the position of the guest window in normal mode.
              * This makes sure that on first use the window opens on the same screen as the normal window was before.
              * This even works with multi-screen. The user just have to move all the normal windows to the target screens
@@ -97,18 +107,29 @@
                 /* Check to which screen the position belongs: */
                 iHostScreen = pDW->screenNumber(p);
-                if (!(   fOk1 /* Valid data */
-                      && fOk2 /* Valid data */
-                      && iHostScreen >= 0 && iHostScreen < m_cHostScreens /* In the host screen bounds? */
-                      && m_screenMap.key(iHostScreen, -1) == -1)) /* Not taken already? */
-                    /* If not, simply pick the next one of the still available host screens: */
-                    iHostScreen = availableScreens.first();
+                /* Revalidate: */
+                fValid =    fOk1 && fOk2 /* Valid data */
+                         && iHostScreen >= 0 && iHostScreen < m_cHostScreens /* In the host screen bounds? */
+                         && m_screenMap.key(iHostScreen, -1) == -1; /* Not taken already? */
             }
-            else
-                /* If not, simply pick the next one of the still available host screens: */
+        }
+
+        if (!fValid)
+        {
+            /* If still not valid, pick the next one
+             * if there is still available host screen: */
+            if (!availableScreens.isEmpty())
+            {
                 iHostScreen = availableScreens.first();
-        }
-        m_screenMap.insert(iGuestScreen, iHostScreen);
-        /* Remove the just selected screen from the list of available: */
-        availableScreens.removeOne(iHostScreen);
+                fValid = true;
+            }
+        }
+
+        if (fValid)
+        {
+            /* Register host screen for the guest screen: */
+            m_screenMap.insert(iGuestScreen, iHostScreen);
+            /* Remove it from the list of available host screens: */
+            availableScreens.removeOne(iHostScreen);
+        }
     }
 
@@ -166,7 +187,8 @@
     QMap<int,int> tmpMap(m_screenMap);
     int iCurrentGuestScreen = tmpMap.key(iRequestedHostScreen, -1);
-    if (iCurrentGuestScreen != -1)
+    if (iCurrentGuestScreen != -1 && tmpMap.contains(iRequestedGuestScreen))
         tmpMap.insert(iCurrentGuestScreen, tmpMap.value(iRequestedGuestScreen));
-    /* Set the new host screen: */
+    else
+        tmpMap.remove(iCurrentGuestScreen);
     tmpMap.insert(iRequestedGuestScreen, iRequestedHostScreen);
 
@@ -188,16 +210,14 @@
         }
     }
-    if (fSuccess)
-    {
-        /* Swap the temporary with the previous map. */
-        m_screenMap = tmpMap;
-    }
-
+    /* Make sure memory requirements matched: */
+    if (!fSuccess)
+        return;
+
+    /* Swap the maps: */
+    m_screenMap = tmpMap;
     /* Update menu actions: */
     updateMenuActions(true);
-
-    /* On success inform the observer: */
-    if (fSuccess)
-        emit sigScreenLayoutChanged();
+    /* Inform the observer: */
+    emit sigScreenLayoutChanged();
 }
 
