MATLAB Answers


A very strange exception when running a mex file - "a corruption of the heap, which indicates a bug in MATLAB.exe"

Asked by Gil
on 19 Jan 2013


I'm running a mex file in Matlab while debugging it in Visual Studio 2010. I mean that I'm running it in Matlab and when the m file reaches the call to the mex file, the focus automatically switches to VS and it stops in a break point i defined.

While I continue running in VS, I get the following message:

"Windows has triggered a breakpoint in MATLAB.exe.

This may be due to a corruption of the heap, which indicates a bug in MATLAB.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while MATLAB.exe has focus.

The output window may have more diagnostic information."

Immediately after that, I get the following message: Unhandled exception at 0x77a240f2 in MATLAB.exe: 0xC0000374: ‏‏Corrupted heap (the exact message is in Hebrew, I translated it to English).

Well, I haven't got the slightest clue as to how to debug it. It doesn't necessarily happen in the same place every time. Does anyone know what it means? What went wrong?

Thanks in advance,



Show 1 older comment

Likely writing through a pointer that has an undefined value (perhaps no longer valid), or free()'ing the wrong location, or writing off the end of an array.

Hi Gil,

Do you use C++ or Fortran?

I get almost the same message. I use Intel Fortran Compiler with Visual Studio 2010.

The strange thing is, that I can run the mexfunction one time. If I run it again, I get that error.

Log in to comment.



1 Answer

Answer by Colin
on 24 Jan 2013

As I wrote in the comment, I got the same error. After a step by step commenting out of code I found the error.

I called the function


without initializing the field 'HAMILTON' in


or with


correctly. Maybe this will help you finding the error.


Log in to comment.

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!