0

When a Mex-function produces a segmentation-fault, a MATLAB System Error window pops-up and choosing Attempt to Continue leads to a lot of useless debugging information which is purred into the command window.

Is it possible to catch these mex-exceptions in Matlab or at least to be able to process them later on?

Unfortunately, following conventional try/catch won't work in this cases:

try
   myMex(input)
catch
   error('Mex failed')
end

Please note that I do not want to catch the error inside the mex.

Community
  • 1
  • 1
JaBe
  • 664
  • 1
  • 8
  • 27
  • I would NOT do this. There is a reason the system error suggests you quit MATLAB and not attempt to continue. There is a pretty good chance you have corrupted memory. Only use Attempt to Continue if you absolutely need to try to save some data. – chappjc Feb 05 '15 at 18:22
  • Yes, there are good reasons not to do this. But I don't intend to use the results afterwards. I want to debug my C code and restarting matlab and reloading all data for every small change is just not practical. – JaBe Feb 05 '15 at 18:28
  • 1
    Debug with a debugger. Attach to MATLAB. [Windows](http://stackoverflow.com/a/27391300/2778484) (I use this method, but you can also tell `mex` to generate debugging info) or [Linux](http://www.mathworks.com/matlabcentral/answers/91741-how-do-i-debug-c-mex-files-under-unix)? – chappjc Feb 05 '15 at 18:46

1 Answers1

1

If you have the source code, you can compile the mex with debug mode (-g flag).
Then, depending on your platform, you can run the mex in debug mode (check out Troubleshoot MEX-Files on MATLAB docs).
If you are running in windows, you can use Visual Studio and select debug->Attach to Process and choose MATLAB. Then you can run your mex file, which will trigger an exception (and a breakpoint) in VS rather than MATLAB.

ThP
  • 2,312
  • 4
  • 19
  • 25
  • Yes, then I can interrupt in VS at the Matlab Breakpoint and do a lot of good debugging stuff. But if I end the Debugging in VS, the segmentation fault in Matlab is still evoked. – JaBe Feb 17 '15 at 15:32