Index: /trunk/src/VBox/Main/src-server/MachineImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/src-server/MachineImpl.cpp	(revision 42787)
+++ /trunk/src/VBox/Main/src-server/MachineImpl.cpp	(revision 42788)
@@ -5945,9 +5945,12 @@
     {
         /* find all attached devices to the appropriate storage controller and detach them all*/
-        MediaData::AttachmentList::const_iterator endList = mMediaData->mAttachments.end();
         MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
-        for (;it != endList; it++)
-        {
-            MediumAttachment *pAttachTemp = *it;
+
+        uint16_t howManyAttach = mMediaData->mAttachments.size();
+
+        for (uint16_t i=0;i<howManyAttach; ++i)
+        {
+            MediumAttachment *pAttachTemp = *it++;
+
             AutoCaller localAutoCaller(pAttachTemp);
             if (FAILED(localAutoCaller.rc())) return localAutoCaller.rc();
@@ -5959,5 +5962,7 @@
                 LONG port = pAttachTemp->getPort();
                 LONG device = pAttachTemp->getDevice();
-                rc = DetachDevice(aName, port, device);
+
+                //rc = DetachDevice(aName, port, device);
+                rc = detachDevice(pAttachTemp, alock, NULL);
                 if (FAILED(rc)) return rc;
             }
