Index: /trunk/src/VBox/Storage/DMG.cpp
===================================================================
--- /trunk/src/VBox/Storage/DMG.cpp	(revision 46612)
+++ /trunk/src/VBox/Storage/DMG.cpp	(revision 46613)
@@ -741,5 +741,5 @@
                 dmgFlushImage(pThis);
 
-            vdIfIoIntFileClose(pThis->pIfIo, pThis->pStorage);
+            rc = vdIfIoIntFileClose(pThis->pIfIo, pThis->pStorage);
             pThis->pStorage = NULL;
         }
@@ -1496,5 +1496,5 @@
                  pszFilename, pVDIfsDisk, pVDIfsImage, penmType));
     int rc;
-    PVDIOSTORAGE pStorage;
+    PVDIOSTORAGE pStorage = NULL;
     uint64_t cbFile, offFtr = 0;
     DMGUDIF Ftr;
@@ -1518,8 +1518,5 @@
     }
     else
-    {
-        vdIfIoIntFileClose(pIfIo, pStorage);
         rc = VERR_VD_DMG_INVALID_HEADER;
-    }
 
     if (RT_SUCCESS(rc))
@@ -1548,5 +1545,6 @@
     }
 
-    vdIfIoIntFileClose(pIfIo, pStorage);
+    if (pStorage)
+        vdIfIoIntFileClose(pIfIo, pStorage);
 
     LogFlowFunc(("returns %Rrc\n", rc));
Index: /trunk/src/VBox/Storage/Parallels.cpp
===================================================================
--- /trunk/src/VBox/Storage/Parallels.cpp	(revision 46612)
+++ /trunk/src/VBox/Storage/Parallels.cpp	(revision 46613)
@@ -158,5 +158,5 @@
                 parallelsFlushImage(pImage);
 
-            vdIfIoIntFileClose(pImage->pIfIo, pImage->pStorage);
+            rc = vdIfIoIntFileClose(pImage->pIfIo, pImage->pStorage);
             pImage->pStorage = NULL;
         }
Index: /trunk/src/VBox/Storage/QCOW.cpp
===================================================================
--- /trunk/src/VBox/Storage/QCOW.cpp	(revision 46612)
+++ /trunk/src/VBox/Storage/QCOW.cpp	(revision 46613)
@@ -996,5 +996,5 @@
                 qcowFlushImage(pImage);
 
-            vdIfIoIntFileClose(pImage->pIfIo, pImage->pStorage);
+            rc = vdIfIoIntFileClose(pImage->pIfIo, pImage->pStorage);
             pImage->pStorage = NULL;
         }
Index: /trunk/src/VBox/Storage/QED.cpp
===================================================================
--- /trunk/src/VBox/Storage/QED.cpp	(revision 46612)
+++ /trunk/src/VBox/Storage/QED.cpp	(revision 46613)
@@ -1142,5 +1142,5 @@
                 qedFlushImage(pImage);
 
-            vdIfIoIntFileClose(pImage->pIfIo, pImage->pStorage);
+            rc = vdIfIoIntFileClose(pImage->pIfIo, pImage->pStorage);
             pImage->pStorage = NULL;
         }
Index: /trunk/src/VBox/Storage/RAW.cpp
===================================================================
--- /trunk/src/VBox/Storage/RAW.cpp	(revision 46612)
+++ /trunk/src/VBox/Storage/RAW.cpp	(revision 46613)
@@ -160,5 +160,5 @@
             }
 
-            vdIfIoIntFileClose(pImage->pIfIo, pImage->pStorage);
+            rc = vdIfIoIntFileClose(pImage->pIfIo, pImage->pStorage);
             pImage->pStorage = NULL;
         }
Index: /trunk/src/VBox/Storage/VD.cpp
===================================================================
--- /trunk/src/VBox/Storage/VD.cpp	(revision 46612)
+++ /trunk/src/VBox/Storage/VD.cpp	(revision 46613)
@@ -2122,6 +2122,6 @@
             /* Read the source data. */
             rc = pImageFrom->Backend->pfnRead(pImageFrom->pBackendData,
-                                                   uOffset, cbThisRead, &IoCtx,
-                                                   &cbThisRead);
+                                              uOffset, cbThisRead, &IoCtx,
+                                              &cbThisRead);
 
             if (   rc == VERR_VD_BLOCK_FREE
@@ -3944,14 +3944,13 @@
 static int vdIOIntClose(void *pvUser, PVDIOSTORAGE pIoStorage)
 {
-    PVDIO pVDIo             = (PVDIO)pvUser;
-
-    int rc = pVDIo->pInterfaceIo->pfnClose(pVDIo->pInterfaceIo->Core.pvUser,
-                                           pIoStorage->pStorage);
-    AssertRC(rc);
-
+    int rc = VINF_SUCCESS;
+    PVDIO pVDIo = (PVDIO)pvUser;
+
+    /* We free everything here, even if closing the file failed for some reason. */
+    rc = pVDIo->pInterfaceIo->pfnClose(pVDIo->pInterfaceIo->Core.pvUser, pIoStorage->pStorage);
     RTAvlrFileOffsetDestroy(pIoStorage->pTreeMetaXfers, vdIOIntTreeMetaXferDestroy, NULL);
     RTMemFree(pIoStorage->pTreeMetaXfers);
     RTMemFree(pIoStorage);
-    return VINF_SUCCESS;
+    return rc;
 }
 
@@ -4690,8 +4689,7 @@
     PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
     int rc = pInterfaceIo->pfnClose(NULL, pIoStorage->pStorage);
-    AssertRC(rc);
 
     RTMemFree(pIoStorage);
-    return VINF_SUCCESS;
+    return rc;
 }
 
@@ -6763,6 +6761,6 @@
 
                 rc = pImageTo->Backend->pfnRead(pImageTo->pBackendData,
-                                                     uOffset, cbThisRead,
-                                                     &IoCtx, &cbThisRead);
+                                                uOffset, cbThisRead,
+                                                &IoCtx, &cbThisRead);
                 if (rc == VERR_VD_BLOCK_FREE)
                 {
@@ -6776,6 +6774,6 @@
                     {
                         rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
-                                                               uOffset, cbThisRead,
-                                                               &IoCtx, &cbThisRead);
+                                                          uOffset, cbThisRead,
+                                                          &IoCtx, &cbThisRead);
                     }
 
Index: /trunk/src/VBox/Storage/VDI.cpp
===================================================================
--- /trunk/src/VBox/Storage/VDI.cpp	(revision 46612)
+++ /trunk/src/VBox/Storage/VDI.cpp	(revision 46613)
@@ -225,5 +225,5 @@
                 vdiFlushImage(pImage);
 
-            vdIfIoIntFileClose(pImage->pIfIo, pImage->pStorage);
+            rc = vdIfIoIntFileClose(pImage->pIfIo, pImage->pStorage);
             pImage->pStorage = NULL;
         }
@@ -1506,25 +1506,27 @@
 
     /* Close the image. */
-    vdiFreeImage(pImage, false);
-
-    /* Rename the file. */
-    rc = vdIfIoIntFileMove(pImage->pIfIo, pImage->pszFilename, pszFilename, 0);
-    if (RT_FAILURE(rc))
-    {
-        /* The move failed, try to reopen the original image. */
-        int rc2 = vdiOpenImage(pImage, pImage->uOpenFlags);
-        if (RT_FAILURE(rc2))
-            rc = rc2;
-
-        goto out;
-    }
-
-    /* Update pImage with the new information. */
-    pImage->pszFilename = pszFilename;
-
-    /* Open the new image. */
-    rc = vdiOpenImage(pImage, pImage->uOpenFlags);
-    if (RT_FAILURE(rc))
-        goto out;
+    rc = vdiFreeImage(pImage, false);
+    if (RT_SUCCESS(rc))
+    {
+        /* Rename the file. */
+        rc = vdIfIoIntFileMove(pImage->pIfIo, pImage->pszFilename, pszFilename, 0);
+        if (RT_FAILURE(rc))
+        {
+            /* The move failed, try to reopen the original image. */
+            int rc2 = vdiOpenImage(pImage, pImage->uOpenFlags);
+            if (RT_FAILURE(rc2))
+                rc = rc2;
+
+            goto out;
+        }
+
+        /* Update pImage with the new information. */
+        pImage->pszFilename = pszFilename;
+
+        /* Open the new image. */
+        rc = vdiOpenImage(pImage, pImage->uOpenFlags);
+        if (RT_FAILURE(rc))
+            goto out;
+    }
 
 out:
@@ -3165,5 +3167,9 @@
 
     if (pStorage)
-        vdIfIoIntFileClose(pIfIo, pStorage);
+    {
+        int rc2 = vdIfIoIntFileClose(pIfIo, pStorage);
+        if (RT_SUCCESS(rc))
+            rc = rc2; /* Propagate error code only if repairing was successful. */
+    }
 
     LogFlowFunc(("returns %Rrc\n", rc));
Index: /trunk/src/VBox/Storage/VHD.cpp
===================================================================
--- /trunk/src/VBox/Storage/VHD.cpp	(revision 46612)
+++ /trunk/src/VBox/Storage/VHD.cpp	(revision 46613)
@@ -560,5 +560,5 @@
                 vhdFlushImage(pImage);
 
-            vdIfIoIntFileClose(pImage->pIfIo, pImage->pStorage);
+            rc = vdIfIoIntFileClose(pImage->pIfIo, pImage->pStorage);
             pImage->pStorage = NULL;
         }
@@ -581,5 +581,5 @@
 
         if (fDelete && pImage->pszFilename)
-            rc = vdIfIoIntFileDelete(pImage->pIfIo, pImage->pszFilename);
+            vdIfIoIntFileDelete(pImage->pIfIo, pImage->pszFilename);
     }
 
@@ -3160,5 +3160,9 @@
 
     if (pStorage)
-        vdIfIoIntFileClose(pIfIo, pStorage);
+    {
+        int rc2 = vdIfIoIntFileClose(pIfIo, pStorage);
+        if (RT_SUCCESS(rc))
+            rc = rc2; /* Propagate status code only when repairing the image was successful. */
+    }
 
     LogFlowFunc(("returns %Rrc\n", rc));
Index: /trunk/src/VBox/Storage/VHDX.cpp
===================================================================
--- /trunk/src/VBox/Storage/VHDX.cpp	(revision 46612)
+++ /trunk/src/VBox/Storage/VHDX.cpp	(revision 46613)
@@ -971,5 +971,5 @@
         if (pImage->pStorage)
         {
-            vdIfIoIntFileClose(pImage->pIfIo, pImage->pStorage);
+            rc = vdIfIoIntFileClose(pImage->pIfIo, pImage->pStorage);
             pImage->pStorage = NULL;
         }
Index: /trunk/src/VBox/Storage/VMDK.cpp
===================================================================
--- /trunk/src/VBox/Storage/VMDK.cpp	(revision 46612)
+++ /trunk/src/VBox/Storage/VMDK.cpp	(revision 46613)
@@ -524,6 +524,6 @@
 
 static void vmdkFreeStreamBuffers(PVMDKEXTENT pExtent);
-static void vmdkFreeExtentData(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
-                               bool fDelete);
+static int vmdkFreeExtentData(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
+                              bool fDelete);
 
 static int vmdkCreateExtents(PVMDKIMAGE pImage, unsigned cExtents);
@@ -2862,8 +2862,15 @@
  * Internal: free the memory used by the extent data structure, optionally
  * deleting the referenced files.
+ *
+ * @returns VBox status code.
+ * @param   pImage    Pointer to the image instance data.
+ * @param   pExtent   The extent to free.
+ * @param   fDelete   Flag whether to delete the backing storage.
  */
-static void vmdkFreeExtentData(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
-                               bool fDelete)
-{
+static int vmdkFreeExtentData(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
+                              bool fDelete)
+{
+    int rc = VINF_SUCCESS;
+
     vmdkFreeGrainDirectory(pExtent);
     if (pExtent->pDescData)
@@ -2875,8 +2882,8 @@
     {
         /* Do not delete raw extents, these have full and base names equal. */
-        vmdkFileClose(pImage, &pExtent->pFile,
-                         fDelete
-                      && pExtent->pszFullname
-                      && strcmp(pExtent->pszFullname, pExtent->pszBasename));
+        rc =vmdkFileClose(pImage, &pExtent->pFile,
+                             fDelete
+                          && pExtent->pszFullname
+                          && strcmp(pExtent->pszFullname, pExtent->pszBasename));
     }
     if (pExtent->pszBasename)
@@ -2891,4 +2898,6 @@
     }
     vmdkFreeStreamBuffers(pExtent);
+
+    return rc;
 }
 
@@ -4321,5 +4330,9 @@
         {
             for (unsigned i = 0 ; i < pImage->cExtents; i++)
-                vmdkFreeExtentData(pImage, &pImage->pExtents[i], fDelete);
+            {
+                int rc2 = vmdkFreeExtentData(pImage, &pImage->pExtents[i], fDelete);
+                if (RT_SUCCESS(rc))
+                    rc = rc2; /* Propogate any error when closing the file. */
+            }
             RTMemFree(pImage->pExtents);
             pImage->pExtents = NULL;
@@ -4327,6 +4340,12 @@
         pImage->cExtents = 0;
         if (pImage->pFile != NULL)
-            vmdkFileClose(pImage, &pImage->pFile, fDelete);
-        vmdkFileCheckAllClose(pImage);
+        {
+            int rc2 = vmdkFileClose(pImage, &pImage->pFile, fDelete);
+            if (RT_SUCCESS(rc))
+                rc = rc2; /* Propogate any error when closing the file. */
+        }
+        int rc2 = vmdkFileCheckAllClose(pImage);
+        if (RT_SUCCESS(rc))
+            rc = rc2; /* Propogate any error when closing the file. */
 
         if (pImage->pGTCache)
@@ -5517,5 +5536,8 @@
             goto rollback;
         /* Close the extent file. */
-        vmdkFileClose(pImage, &pExtent->pFile, false);
+        rc = vmdkFileClose(pImage, &pExtent->pFile, false);
+        if (RT_FAILURE(rc))
+            goto rollback;
+
         /* Rename the extent file. */
         rc = vdIfIoIntFileMove(pImage->pIfIo, pExtent->pszFullname, apszNewName[i], 0);
@@ -5526,5 +5548,7 @@
     }
     /* Release all old stuff. */
-    vmdkFreeImage(pImage, false);
+    rc = vmdkFreeImage(pImage, false);
+    if (RT_FAILURE(rc))
+        goto rollback;
 
     fImageFreed = true;
