MATLAB: Matlab crashes while debugging mex file

c codedebugdebug c codeMATLABmex

I am writing C code and compiling to mex files. I compile with the -g option and debug with Visual Studio (I use the Tools -> Attach to Process option in VS). However, when there is an error in the code and I stop the run (with the Stop Debugging button) Matlab crashes. This also happens if I simply run a mex file with an error and Visual Studio is not attached to Matlab.
Is there a way to debug C code without Matlab crashing?
Thanks a lot,
Michael.

Best Answer

With mex files, you are potentially corrupting the internal state of MATLAB. There is no protection layer or emulation layer: you get direct access to pointers being used by MATLAB and if (perhaps due to bugs) your code doesn't play well with MATLAB, then MATLAB will crash.