Mex file compiles in Visual Studio but crashes MATLAB, getting error in debug.

7 views (last 30 days)
HI Matlab central community. I tried to ask this question in StackOverflow but I could not resolve it. I compile a mex file in VIsual Studio 2013, which is very basic:
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
mexPrintf("Hello! :)\n");
This compiles as a mexw64 succesfully. I then open up MATLAB v2016a, and try to run mcx() with no input parameters and mcx(cfg) where cfg has 1,2 or 3 parameters. Each different scenario cases MATLAB to crash, end prematurely. I attach visual studio to MATLAB, and I get the error:
'MATLAB.exe' (Win32): Loaded 'C:\Users\tub08918\Google Drive\Patil Lab\AlexsFolderPleaseVisitMe\RamanMonteCarlo\mcx-2017.3-src\mcx\vsproj\mcx\x64\Debug\mcx.mexw64'. Symbols loaded.
First-chance exception at 0x00000000000260A8 in MATLAB.exe: 0xC0000005: Access violation executing location 0x00000000000260A8.
> throw_segv_longjmp_seh_filter()
throw_segv_longjmp_seh_filter(): invoking THROW_SEGV_LONGJMP SEH filter
> throwSegvLongjmpFilter()
MATLAB.exe has triggered a breakpoint.
Now the aim is to compile a file with CUDA capabilities, bu I can't even figure out what is causing this very simple ne-line code to fail. My props file comes from My build is x64 and I made sure to compile as x64. I tried using a new project that didnt help, so I am out of ideas why this simple code would be compiling as a mex file but failing when called from MATLAB. Interstingly, if I leave mexFunction blank, then MATLAB does not crash. IF I have some easy operations, like 2+2, MATLAB does not failk. It seems to happen when I call any function built into c++ or from mex.h

Accepted Answer

Philip Borghesani
Philip Borghesani on 5 Apr 2017
Edited: Philip Borghesani on 5 Apr 2017
First suggestion build with the mex command instead of directly with visual studio. Make sure that works. Then check your compile options against those displayed with mex -v . If this was x32 then I would blame the calling convention, MATLAB requires cdecl default.
Some first chance access violations are expected due to the use of Java. If the mex file works without a debugger attached and continue works (might require multiple and frequent continues... disable first chance access violation checking) then there are no problems with the code.

Sign in to comment.

More Answers (0)


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!