diff --git a/mex/sources/bytecode/bytecode.cc b/mex/sources/bytecode/bytecode.cc
index e05fb853b286074e8e7dfc9fb688218190d80ba7..2ab41b4561040426ea77bf78f3b7d75281128e28 100644
--- a/mex/sources/bytecode/bytecode.cc
+++ b/mex/sources/bytecode/bytecode.cc
@@ -723,6 +723,8 @@ mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
         }
       catch (GeneralExceptionHandling &feh)
         {
+          // Release the lock on dynamic.bin for MATLAB+Windows, see #1815
+          interprete.Close_SaveCode();
           mexErrMsgTxt(feh.GetErrorMsg().c_str());
         }
     }
@@ -734,6 +736,8 @@ mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
         }
       catch (GeneralExceptionHandling &feh)
         {
+          // Release the lock on dynamic.bin for MATLAB+Windows, see #1815
+          interprete.Close_SaveCode();
           mexErrMsgTxt(feh.GetErrorMsg().c_str());
         }
     }