Matlab crashes when a C mex file is called

2 visualizaciones (últimos 30 días)
Siyomnqoba
Siyomnqoba el 5 de Oct. de 2015
Editada: James Tursa el 5 de Oct. de 2015
the following C code accepts a link structure and uses it to decode a variable length sequence.
/*Unravel.c*/
#include "mex.h"
void unravel(unsigned short *hx, double *link, double *x, double xsz, int hxsz)
{
int i = 15, j = 0, k = 0, n = 0;
while(xsz - k)
{
if (*(link + n)>0)
{
if ((*(hx + j)>>i) & 0x0001)
{
n = *(link + n);
}
else n = *(link + n) - 1;
if (i) i--; else {j++;i = 15;}
if (j> hxsz)
mexErrMsgTxt("out of code bits ???");
}
else
{
*(x + k++) = -*(link + n);
n = 0;
}
}
if (k == xsz - 1)
*(x + k++) = -*(link + n);
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double *link, *x, xsz;
unsigned short *hx;
int hxsz;
/*check for reasonableness*/
if(nrhs !=3)
mexErrMsgTxt("3 inputs required.");
else if (nlhs > 1)
mexErrMsgTxt("Too many output arguments");
/*is the last input a scalar*/
if (!mxIsDouble(prhs[2]) || mxIsComplex(prhs[2]) || mxGetN(prhs[2] * mxGetM(prhs[2]) != 1)
{
mexErrMsgTxt("input XSIZE must be a scalar.");
}
hx = mxGetPr(prhs[0]);
link - mxGetPr(prhs[1]);
xsz = mxGetScalar(prhs[2]);
hxsz = mxGetM(prhs[0]);
unravel(hx, link, x, xsz, hxsz);
}
It is being called as follows
x = unravel(y.code,link,m*n);

Respuestas (1)

James Tursa
James Tursa el 5 de Oct. de 2015
Editada: James Tursa el 5 de Oct. de 2015
A few observations:
unsigned short *hx;
:
hx = mxGetPr(prhs[0]);
The above code will only work properly if prhs[0] is passed in as a uint16 class variable. You should check for this at the front of your mex routine.
double *link, *x, xsz;
:
link - mxGetPr(prhs[1]);
The above lines will not work at all since you inadvertently used a - instead of a = in the second line. And even after you fix that, be advised that prhs[1] will need to be passed in as a double class variable, so you should check for that. The way you currently have things coded, link is not initialized and gets passed to your unravel function, where it gets dereferenced (one likely cause of the crash). In fact, I would not be surprised that this line in and of itself could cause a crash since the result of the calculation is quite likely an invalid address.
And then this call:
unravel(hx, link, x, xsz, hxsz);
As near as I can tell x is never initialized to anything. So x is not initialized and gets passed to your unravel function, where it gets dereferenced (another likely cause of the crash).
If x is supposed to be an output, then you need to create an appropriate plhs[0] output (class and size) and then do this prior to your unravel call:
plhs[0] = mxCreateWHATEVER(WHATEVER) <-- fill in with appropriate creation routine and arguments
x = mxGetPr(plhs[0]);

Categorías

Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by