Index: /trunk/src/VBox/Main/src-server/MachineImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/src-server/MachineImpl.cpp	(revision 42822)
+++ /trunk/src/VBox/Main/src-server/MachineImpl.cpp	(revision 42823)
@@ -5945,9 +5945,13 @@
     {
         /* find all attached devices to the appropriate storage controller and detach them all */
-        size_t howManyAttach = mMediaData->mAttachments.size();
-
-        for (size_t i = 0; i < howManyAttach; ++i)
-        {
-            MediumAttachment *pAttachTemp = mMediaData->mAttachments.front();
+        // make a temporary list because detachDevice invalidates iterators into
+        // mMediaData->mAttachments
+        MediaData::AttachmentList llAttachments2 = mMediaData->mAttachments;
+
+        for (MediaData::AttachmentList::iterator it = llAttachments2.begin();
+        it != llAttachments2.end();
+        ++it)
+        {
+            MediumAttachment *pAttachTemp = *it;
 
             AutoCaller localAutoCaller(pAttachTemp);
@@ -5958,8 +5962,4 @@
             if (pAttachTemp->getControllerName() == aName)
             {
-                LONG port = pAttachTemp->getPort();
-                LONG device = pAttachTemp->getDevice();
-
-                //rc = DetachDevice(aName, port, device);
                 rc = detachDevice(pAttachTemp, alock, NULL);
                 if (FAILED(rc)) return rc;
