Index: /trunk/src/VBox/Main/MediumImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/MediumImpl.cpp	(revision 30313)
+++ /trunk/src/VBox/Main/MediumImpl.cpp	(revision 30314)
@@ -5433,4 +5433,8 @@
     }
 
+    // deregister the task registered in createDiffStorage()
+    Assert(m->numCreateDiffTasks != 0);
+    --m->numCreateDiffTasks;
+
     if (task.isAsync())
     {
@@ -5446,8 +5450,4 @@
         if (task.m_pfNeedsSaveSettings)
             *task.m_pfNeedsSaveSettings = fNeedsSaveSettings;
-
-    /* deregister the task registered in createDiffStorage() */
-    Assert(m->numCreateDiffTasks != 0);
-    --m->numCreateDiffTasks;
 
     /* Note that in sync mode, it's the caller's responsibility to
Index: /trunk/src/VBox/Main/VirtualBoxImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/VirtualBoxImpl.cpp	(revision 30313)
+++ /trunk/src/VBox/Main/VirtualBoxImpl.cpp	(revision 30314)
@@ -3496,5 +3496,7 @@
 
 /**
- *  Helper function to write out the configuration tree.
+ *  Helper function which actually writes out VirtualBox.xml, the main configuration file.
+ *  Gets called from the public VirtualBox::SaveSettings() as well as from various other
+ *  places internally when settings need saving.
  *
  *  @note Caller must have locked the VirtualBox object for writing!
@@ -3569,6 +3571,6 @@
         settings::MediaList hardDisksTemp;
         for (MediaList::const_iterator it = m->ollHardDisks.begin();
-                it != m->ollHardDisks.end();
-                ++it)
+             it != m->ollHardDisks.end();
+             ++it)
         {
             settings::Medium med;
@@ -3581,6 +3583,6 @@
         settings::MediaList dvdsTemp;
         for (MediaList::const_iterator it = m->ollDVDImages.begin();
-                it != m->ollDVDImages.end();
-                ++it)
+             it != m->ollDVDImages.end();
+             ++it)
         {
             settings::Medium med;
@@ -3593,6 +3595,6 @@
         settings::MediaList floppiesTemp;
         for (MediaList::const_iterator it = m->ollFloppyImages.begin();
-                it != m->ollFloppyImages.end();
-                ++it)
+             it != m->ollFloppyImages.end();
+             ++it)
         {
             settings::Medium med;
@@ -4588,6 +4590,6 @@
 	{
             listeners.Add(*pp);
-        }                   
-#endif  
+        }
+#endif
     }
 
@@ -4613,5 +4615,5 @@
 
         /**
-         * Would be just handleCallback(cbI) in an ideal world, unfortunately our 
+         * Would be just handleCallback(cbI) in an ideal world, unfortunately our
 	 * consumers want to be invoked via IDispatch, thus going the hard way.
          */
@@ -4769,5 +4771,8 @@
     if (aSaveRegistry)
     {
+        AutoWriteLock vboxLock(this COMMA_LOCKVAL_SRC_POS);
         rc = saveSettings();
+        vboxLock.release();
+
         if (FAILED(rc))
             unregisterDHCPServer(aDHCPServer, false /* aSaveRegistry */);
@@ -4811,5 +4816,8 @@
     if (aSaveRegistry)
     {
+        AutoWriteLock vboxLock(this COMMA_LOCKVAL_SRC_POS);
         rc = saveSettings();
+        vboxLock.release();
+
         if (FAILED(rc))
             registerDHCPServer(aDHCPServer, false /* aSaveRegistry */);
