Index: /trunk/src/VBox/Storage/QED.cpp
===================================================================
--- /trunk/src/VBox/Storage/QED.cpp	(revision 51624)
+++ /trunk/src/VBox/Storage/QED.cpp	(revision 51625)
@@ -5,5 +5,5 @@
 
 /*
- * Copyright (C) 2011-2013 Oracle Corporation
+ * Copyright (C) 2011-2014 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -1181,4 +1181,18 @@
 
     /*
+     * Create the L2 cache before opening the image so we can call qedFreeImage()
+     * even if opening the image file fails.
+     */
+    rc = qedL2TblCacheCreate(pImage);
+    if (RT_FAILURE(rc))
+    {
+        rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
+                       N_("Qed: Creating the L2 table cache for image '%s' failed"),
+                       pImage->pszFilename);
+
+        goto out;
+    }
+
+    /*
      * Open the image.
      */
@@ -1257,15 +1271,8 @@
                         {
                             qedTableConvertToHostEndianess(pImage->paL1Table, pImage->cTableEntries);
-                            rc = qedL2TblCacheCreate(pImage);
-                            if (RT_SUCCESS(rc))
-                            {
-                                /* If the consistency check succeeded, clear the flag by flushing the image. */
-                                if (Header.u64FeatureFlags & QED_FEATURE_NEED_CHECK)
-                                    rc = qedFlushImage(pImage);
-                            }
-                            else
-                                rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
-                                               N_("Qed: Creating the L2 table cache for image '%s' failed"),
-                                               pImage->pszFilename);
+
+                            /* If the consistency check succeeded, clear the flag by flushing the image. */
+                            if (Header.u64FeatureFlags & QED_FEATURE_NEED_CHECK)
+                                rc = qedFlushImage(pImage);
                         }
                         else
