Index: /trunk/src/VBox/Main/MachineImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/MachineImpl.cpp	(revision 31343)
+++ /trunk/src/VBox/Main/MachineImpl.cpp	(revision 31344)
@@ -4090,4 +4090,5 @@
     std::list<Utf8Str>          llFilesToDelete;
     ComObjPtr<Progress>         pProgress;
+    bool                        fNeedsGlobalSaveSettings;
 };
 
@@ -4100,6 +4101,4 @@
 
     AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
-
-    bool fNeedsGlobalSaveSettings = false;
 
     HRESULT rc = checkStateDependency(MutableStateDep);
@@ -4116,4 +4115,6 @@
     // collect files to delete
     pTask->llFilesToDelete = mData->llFilesToDelete;            // saved states pushed here by Unregister()
+
+    pTask->fNeedsGlobalSaveSettings = false;
     for (size_t i = 0; i < sfaMedia.size(); ++i)
     {
@@ -4126,5 +4127,6 @@
         // close the medium now; if that succeeds, then that means the medium is no longer
         // in use and we can add it to the list of files to delete
-        rc = pMedium->close(&fNeedsGlobalSaveSettings, mediumAutoCaller);
+        rc = pMedium->close(&pTask->fNeedsGlobalSaveSettings,
+                            mediumAutoCaller);
         if (SUCCEEDED(rc))
             pTask->llFilesToDelete.push_back(bstrLocation);
@@ -4150,12 +4152,4 @@
 
     pTask->pProgress.queryInterfaceTo(aProgress);
-
-    alock.release();
-
-    if (fNeedsGlobalSaveSettings)
-    {
-        AutoWriteLock vboxlock(mParent COMMA_LOCKVAL_SRC_POS);
-        mParent->saveSettings();
-    }
 
     if (RT_FAILURE(vrc))
@@ -4296,4 +4290,12 @@
                 RTDirRemove(settingsDir.c_str());
         }
+    }
+
+    alock.release();
+
+    if (task.fNeedsGlobalSaveSettings)
+    {
+        AutoWriteLock vboxlock(mParent COMMA_LOCKVAL_SRC_POS);
+        mParent->saveSettings();
     }
 
