Index: /trunk/src/VBox/Main/MachineImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/MachineImpl.cpp	(revision 31222)
+++ /trunk/src/VBox/Main/MachineImpl.cpp	(revision 31223)
@@ -4126,7 +4126,9 @@
                     llMedia.push_back(pMedium);
 
-                detachDevice(pAttach,
-                             alock,
-                             NULL /* pfNeedsSaveSettings */);
+                HRESULT rc = detachDevice(pAttach,
+                                          alock,
+                                          NULL /* pfNeedsSaveSettings */);
+                if (FAILED(rc))
+                    break;
             };
         }
@@ -4137,4 +4139,10 @@
     }
 
+    if (FAILED(rc))
+    {
+        rollbackMedia();
+        return rc;
+    }
+
     // commit all the media changes made above
     commitMedia();
@@ -4148,5 +4156,4 @@
     {
         // now go thru the list of attached media reported by prepareUnregister() and close them all
-        size_t u = 0;
         for (MediaList::const_iterator it = llMedia.begin();
              it != llMedia.end();
@@ -4159,11 +4166,17 @@
             if (FAILED(autoCaller2.rc())) return autoCaller2.rc();
 
-            pMedium->close(NULL /*fNeedsSaveSettings*/,     // we'll call saveSettings() in any case below
-                           autoCaller2);
+            ErrorInfoKeeper eik;
+            HRESULT rc = pMedium->close(NULL /*fNeedsSaveSettings*/,     // we'll call saveSettings() in any case below
+                                        autoCaller2);
                 // this uninitializes the medium
 
-            // report the path to the caller
-            llFilesForCaller.push_back(bstrFile);
-            ++u;
+            if (rc == VBOX_E_OBJECT_IN_USE)
+                // can happen if the medium was still attached to another machine;
+                // do not report the file to the caller then, but don't report
+                // an error either
+                eik.setNull();
+            else if (SUCCEEDED(rc))
+                // report the path to the caller
+                llFilesForCaller.push_back(bstrFile);
         }
     }
