Index: /trunk/src/VBox/Main/MediumImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/MediumImpl.cpp	(revision 29939)
+++ /trunk/src/VBox/Main/MediumImpl.cpp	(revision 29940)
@@ -1441,10 +1441,10 @@
     m->type = aType;
 
+    mlock.release();
+
     // saveSettings needs vbox lock
-    ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
-    mlock.leave();
     AutoWriteLock alock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
 
-    HRESULT rc = pVirtualBox->saveSettings();
+    HRESULT rc = m->pVirtualBox->saveSettings();
 
     return rc;
@@ -1559,6 +1559,5 @@
     if (FAILED(autoCaller.rc())) return autoCaller.rc();
 
-    /* VirtualBox::saveSettings() needs a write lock */
-    AutoMultiWriteLock2 alock(m->pVirtualBox, this COMMA_LOCKVAL_SRC_POS);
+    AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
 
     if (m->pParent.isNull())
@@ -1570,4 +1569,9 @@
     {
         m->autoReset = !!aAutoReset;
+
+        mlock.release();
+
+        // saveSettings needs vbox lock
+        AutoWriteLock alock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
 
         return m->pVirtualBox->saveSettings();
@@ -1980,6 +1984,5 @@
     if (FAILED(autoCaller.rc())) return autoCaller.rc();
 
-    /* VirtualBox::saveSettings() needs a write lock */
-    AutoMultiWriteLock2 alock(m->pVirtualBox, this COMMA_LOCKVAL_SRC_POS);
+    AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
 
     switch (m->state)
@@ -2003,4 +2006,8 @@
         it->second = aValue;
 
+    mlock.release();
+
+    // saveSettings needs vbox lock
+    AutoWriteLock alock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
     HRESULT rc = m->pVirtualBox->saveSettings();
 
@@ -2051,6 +2058,5 @@
     if (FAILED(autoCaller.rc())) return autoCaller.rc();
 
-    /* VirtualBox::saveSettings() needs a write lock */
-    AutoMultiWriteLock2 alock(m->pVirtualBox, this COMMA_LOCKVAL_SRC_POS);
+    AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
 
     com::SafeArray<IN_BSTR> names(ComSafeArrayInArg(aNames));
@@ -2081,4 +2087,8 @@
     }
 
+    mlock.release();
+
+    // saveSettings needs vbox lock
+    AutoWriteLock alock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
     HRESULT rc = m->pVirtualBox->saveSettings();
 
@@ -3449,7 +3459,5 @@
 
         alock.leave();
-
         vrc = RTSemEventMultiWait(m->queryInfoSem, RT_INDEFINITE_WAIT);
-
         alock.enter();
 
@@ -5161,5 +5169,5 @@
 
         /* unlock before the potentially lengthy operation */
-        thisLock.leave();
+        thisLock.release();
 
         try
@@ -5295,5 +5303,5 @@
         /* the two media are now protected by their non-default states;
          * unlock the media before the potentially lengthy operation */
-        mediaLock.leave();
+        mediaLock.release();
 
         try
@@ -5412,5 +5420,5 @@
         if (fNeedsSaveSettings)
         {
-            mediaLock.leave();
+            mediaLock.release();
             AutoWriteLock vboxlock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
             m->pVirtualBox->saveSettings();
@@ -5817,5 +5825,5 @@
 
             /* unlock before the potentially lengthy operation */
-            thisLock.leave();
+            thisLock.release();
 
             /* ensure the target directory exists */
@@ -6238,5 +6246,5 @@
 
             /* unlock before the potentially lengthy operation */
-            thisLock.leave();
+            thisLock.release();
 
             vrc = VDCompact(hdd, VD_LAST_IMAGE, task.mVDOperationIfaces);
