Index: /trunk/src/VBox/Main/MediumImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/MediumImpl.cpp	(revision 24508)
+++ /trunk/src/VBox/Main/MediumImpl.cpp	(revision 24509)
@@ -71,5 +71,5 @@
             const Guid &aSnapshotId = Guid::Empty)
         : machineId(aMachineId),
-          inCurState(aSnapshotId.isEmpty())
+          fInCurState(aSnapshotId.isEmpty())
     {
         if (!aSnapshotId.isEmpty())
@@ -78,5 +78,5 @@
 
     Guid machineId;
-    bool inCurState : 1;
+    bool fInCurState : 1;
     GuidList llSnapshotIds;
 };
@@ -1793,6 +1793,6 @@
             /* if the medium is attached to the machine in the current state, we
              * return its ID as the first element of the array */
-            if (it->inCurState)
-                ++ size;
+            if (it->fInCurState)
+                ++size;
 
             if (size > 0)
@@ -1801,6 +1801,6 @@
 
                 size_t j = 0;
-                if (it->inCurState)
-                    it->machineId.toUtf16().detachTo(&snapshotIds [j ++]);
+                if (it->fInCurState)
+                    it->machineId.toUtf16().detachTo(&snapshotIds[j++]);
 
                 for (BackRef::GuidList::const_iterator jt = it->llSnapshotIds.begin();
@@ -1808,5 +1808,5 @@
                      ++jt, ++j)
                 {
-                     (*jt).toUtf16().detachTo(&snapshotIds [j]);
+                     (*jt).toUtf16().detachTo(&snapshotIds[j]);
                 }
             }
@@ -2689,12 +2689,17 @@
     }
 
+    // if the caller has not supplied a snapshot ID, then we're attaching
+    // to a machine a medium which represents the machine's current state,
+    // so set the flag
     if (aSnapshotId.isEmpty())
     {
         /* sanity: no duplicate attachments */
-        AssertReturn(!it->inCurState, E_FAIL);
-        it->inCurState = true;
+        AssertReturn(!it->fInCurState, E_FAIL);
+        it->fInCurState = true;
 
         return S_OK;
     }
+
+    // otherwise: a snapshot medium is being attached
 
     /* sanity: no duplicate attachments */
@@ -2715,4 +2720,5 @@
 
     it->llSnapshotIds.push_back(aSnapshotId);
+    it->fInCurState = false;
 
     LogFlowThisFuncLeave();
@@ -2747,5 +2753,5 @@
     {
         /* remove the current state attachment */
-        it->inCurState = false;
+        it->fInCurState = false;
     }
     else
@@ -2760,5 +2766,5 @@
 
     /* if the backref becomes empty, remove it */
-    if (it->inCurState == false && it->llSnapshotIds.size() == 0)
+    if (it->fInCurState == false && it->llSnapshotIds.size() == 0)
         m->backRefs.erase(it);
 
@@ -2783,5 +2789,5 @@
     {
         const BackRef &ref = *it2;
-        LogFlowThisFunc(("  Backref from machine {%RTuuid}\n", ref.machineId.raw()));
+        LogFlowThisFunc(("  Backref from machine {%RTuuid} (fInCurState: %d)\n", ref.machineId.raw(), ref.fInCurState));
 
         for (BackRef::GuidList::const_iterator jt2 = it2->llSnapshotIds.begin();
@@ -2874,5 +2880,5 @@
         std::find_if(m->backRefs.begin(), m->backRefs.end(),
                         BackRef::EqualsTo(aMachineId));
-    return it != m->backRefs.end() && it->inCurState;
+    return it != m->backRefs.end() && it->fInCurState;
 }
 
@@ -3190,5 +3196,5 @@
      * (the code calling prepareDiscard() should detach). */
     AssertReturn(m->backRefs.size() == 1, E_FAIL);
-    AssertReturn(!m->backRefs.front().inCurState, E_FAIL);
+    AssertReturn(!m->backRefs.front().fInCurState, E_FAIL);
     AssertReturn(m->backRefs.front().llSnapshotIds.size() == 1, E_FAIL);
 
@@ -4234,5 +4240,5 @@
          it != m->backRefs.end(); ++ it)
     {
-        if (it->inCurState)
+        if (it->fInCurState)
         {
             /* Note: when a VM snapshot is being taken, all normal hard disks
Index: /trunk/src/VBox/Main/SnapshotImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/SnapshotImpl.cpp	(revision 24508)
+++ /trunk/src/VBox/Main/SnapshotImpl.cpp	(revision 24509)
@@ -2128,4 +2128,8 @@
                         // do not lock, prepareDiscared() has a write lock which will hang otherwise
 
+#ifdef DEBUG
+                pHD->dumpBackRefs();
+#endif
+
                 Medium::MergeChain *chain = NULL;
 
