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

Thread Subject:
Built Mex crashes in matlab, breakpoint not reached in Visual Studio debugging ...

Subject: Built Mex crashes in matlab, breakpoint not reached in Visual Studio debugging ...

From: Raphael

Date: 13 Jun, 2013 02:24:09

Message: 1 of 2

Hi everyone,
I've been trying to build a mex file to get some fast C++ computational routine (3 * for loops) running instead of matlab loops.

The mex builds OK, but matlab crashes when I try to use it in the M console.
I just can't figure out how to debug it, because breakpoints are never reached wherever I put them in the cpp in Visual Studio.

I'm a beginner with this kind of stuff, and tried to write the code following matlab help and examples given in matlab/extern. This code is about creating a 3D matrix output with computation that uses 3 * 3vectors (3 different sizes, corresponding to the 3 dimensions, and the 3 loops) as input.

Here it is, it's short ! So if someone can look at it, any help would be greatly appreciated.


#include <cmath>
#include "mex.h"

/* The computational routine */
void comp_R( double *Xobs, double *Yobs, double *Zobs,
double *x, double *y, double *z,
double *X, double *Y, double *Z, double *R,
mwSize M, mwSize T, mwSize GS)
{
mwSize m,t,gs;
    for(m=0; m < M ; m++)
{
for(t=0; t < T ; t++)
{
for(gs=0; gs < GS ; gs++)
{
R[gs*(T*M)+t*M+m]=sqrt((Xobs[gs]+(x[t]-x[0])-X[m])*(Xobs[gs]+(x[t]-x[0])-X[m])+(Yobs[gs]+(y[t]-y[0])-Y[m])*(Yobs[gs]+(y[t]-y[0])-Y[m])+(Zobs[gs]+(z[t]-z[0])-Z[m])*(Zobs[gs]+(z[t]-z[0])-Z[m]));
/*delay[gs][t][m]=R[gs][t][m]/c;*/
}
}
}
}

/* The gateway function */
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
/* check number of arg */
if(nrhs!=9) {
mexErrMsgIdAndTxt("MyToolbox:vec_geom:nrhs","Nine inputs required.");
}
if(nlhs!=1) {
mexErrMsgIdAndTxt("MyToolbox:vec_geom:nlhs","One output required.");
}

/* variables initialisation*/

double *xMatrix,*yMatrix,*zMatrix,*XMatrix,*YMatrix,*ZMatrix,*XobsMatrix,*YobsMatrix,*ZobsMatrix; // input matrices
double *outMatrix; // output matrix
mwSize n_GS,n_T,n_M; // grid size, time samples, number of microphones
mwSignedIndex dims[3];

/* create a pointer to the real data in the input matrix and get dimensions of the input matrix */
XobsMatrix = mxGetPr(prhs[0]); n_GS = mxGetN(prhs[0]);
YobsMatrix = mxGetPr(prhs[1]);
ZobsMatrix = mxGetPr(prhs[2]);
xMatrix = mxGetPr(prhs[3]); n_T = mxGetN(prhs[3]);
yMatrix = mxGetPr(prhs[4]);
zMatrix = mxGetPr(prhs[5]);
XMatrix = mxGetPr(prhs[6]); n_M = mxGetN(prhs[6]);
YMatrix = mxGetPr(prhs[7]);
ZMatrix = mxGetPr(prhs[8]);

/* create output matrix */
dims[0] = n_GS ;
dims[1] = n_T ;
dims[2] = n_M ;
plhs[0] = mxCreateNumericArray(3, dims, mxSINGLE_CLASS, mxREAL);
/*, delay(boost::extents[GS][T][M])*/

/* get a pointer to the real data in the output matrix */
    outMatrix = mxGetPr(plhs[0]);

/* call the computational routine */
comp_R(XobsMatrix,YobsMatrix,ZobsMatrix,xMatrix,yMatrix,zMatrix,XMatrix,YMatrix,ZMatrix,outMatrix,n_GS,n_T,n_M);
}


(Just to precise: I did the debugging with another version automatically provided by matlab coder (but it was slower than the original .m, that's why I tried to write it down by myself)

Subject: Built Mex crashes in matlab, breakpoint not reached in Visual Studio debugging ...

From: James Tursa

Date: 13 Jun, 2013 08:19:13

Message: 2 of 2

"Raphael " <rventura@ec-lyon.fr> wrote in message <kpbag9$6od$1@newscl01ah.mathworks.com>...
> Hi everyone,
> I've been trying to build a mex file to get some fast C++ computational routine (3 * for loops) running instead of matlab loops.
>
> The mex builds OK, but matlab crashes when I try to use it in the M console.
> I just can't figure out how to debug it, because breakpoints are never reached wherever I put them in the cpp in Visual Studio.
>
> I'm a beginner with this kind of stuff, and tried to write the code following matlab help and examples given in matlab/extern. This code is about creating a 3D matrix output with computation that uses 3 * 3vectors (3 different sizes, corresponding to the 3 dimensions, and the 3 loops) as input.
>
> Here it is, it's short ! So if someone can look at it, any help would be greatly appreciated.
>
>
> #include <cmath>
> #include "mex.h"
>
> /* The computational routine */
> void comp_R( double *Xobs, double *Yobs, double *Zobs,
> double *x, double *y, double *z,
> double *X, double *Y, double *Z, double *R,
> mwSize M, mwSize T, mwSize GS)
> {
> mwSize m,t,gs;
> for(m=0; m < M ; m++)
> {
> for(t=0; t < T ; t++)
> {
> for(gs=0; gs < GS ; gs++)
> {
> R[gs*(T*M)+t*M+m]=sqrt((Xobs[gs]+(x[t]-x[0])-X[m])*(Xobs[gs]+(x[t]-x[0])-X[m])+(Yobs[gs]+(y[t]-y[0])-Y[m])*(Yobs[gs]+(y[t]-y[0])-Y[m])+(Zobs[gs]+(z[t]-z[0])-Z[m])*(Zobs[gs]+(z[t]-z[0])-Z[m]));
> /*delay[gs][t][m]=R[gs][t][m]/c;*/
> }
> }
> }
> }
>
> /* The gateway function */
> void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
> {
> /* check number of arg */
> if(nrhs!=9) {
> mexErrMsgIdAndTxt("MyToolbox:vec_geom:nrhs","Nine inputs required.");
> }
> if(nlhs!=1) {
> mexErrMsgIdAndTxt("MyToolbox:vec_geom:nlhs","One output required.");
> }
>
> /* variables initialisation*/
>
> double *xMatrix,*yMatrix,*zMatrix,*XMatrix,*YMatrix,*ZMatrix,*XobsMatrix,*YobsMatrix,*ZobsMatrix; // input matrices
> double *outMatrix; // output matrix
> mwSize n_GS,n_T,n_M; // grid size, time samples, number of microphones
> mwSignedIndex dims[3];
>
> /* create a pointer to the real data in the input matrix and get dimensions of the input matrix */
> XobsMatrix = mxGetPr(prhs[0]); n_GS = mxGetN(prhs[0]);
> YobsMatrix = mxGetPr(prhs[1]);
> ZobsMatrix = mxGetPr(prhs[2]);
> xMatrix = mxGetPr(prhs[3]); n_T = mxGetN(prhs[3]);
> yMatrix = mxGetPr(prhs[4]);
> zMatrix = mxGetPr(prhs[5]);
> XMatrix = mxGetPr(prhs[6]); n_M = mxGetN(prhs[6]);
> YMatrix = mxGetPr(prhs[7]);
> ZMatrix = mxGetPr(prhs[8]);
>
> /* create output matrix */
> dims[0] = n_GS ;
> dims[1] = n_T ;
> dims[2] = n_M ;
> plhs[0] = mxCreateNumericArray(3, dims, mxSINGLE_CLASS, mxREAL);
> /*, delay(boost::extents[GS][T][M])*/
>
> /* get a pointer to the real data in the output matrix */
> outMatrix = mxGetPr(plhs[0]);
>
> /* call the computational routine */
> comp_R(XobsMatrix,YobsMatrix,ZobsMatrix,xMatrix,yMatrix,zMatrix,XMatrix,YMatrix,ZMatrix,outMatrix,n_GS,n_T,n_M);
> }
>
>
> (Just to precise: I did the debugging with another version automatically provided by matlab coder (but it was slower than the original .m, that's why I tried to write it down by myself)

See my reply in Answers. Basically, you have defined plhs[0] to be a single class variable (mxSINGLE_CLASS) but you are accessing it downstream in the code as a double array. So you are probably walking off the end of the allocated memory for this array.

James Tursa

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us