Matlab MEX File: Program Crashes in the second run: Access Violation in Read

I have a C++ code that I am trying to interface with Matlab. My mex file runs fine in the first run but crashes in the second run. However, if I clear all the variables in the Matlab before execution (using clear all) program never crashes. So I have a question in this: 1. Can mex function takes variables from the Matlab workspace without using some special functions? Am I doing it somehow in my code, unintentionally?

  1. I have a posted the mex function that I wrote. It has a one dimensional vector called "block" that is read inside the C++ function called sphere_detector. For the present problem the block size is 1x1920 and it is read in the chunk of 16 elements inside the sphere_detector. Program crashed when I read the SECOND chunk of 16 elements. The first element that I read in the chunk will throw this error:

First-chance exception at 0x000007fefac7206f (sphere_decoder.mexw64) in MATLAB.exe: 0xC0000005: Access violation reading location 0xffffffffffffffff. MATLAB.exe has triggered a breakpoint

I checked my block vector, it should have all the values initialized and it has that. So, I am little confused as to why I am facing this problem.

I am using Matlab 2010a and Visual Studio 2010 Professional.

Here is the mex function:

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
double *mod_scheme, *Mt, *Mr, *block_length, *SNR;
mod_scheme = mxGetPr(prhs[0]);
Mt = mxGetPr(prhs[1]);
Mr = mxGetPr(prhs[2]);
block_length = mxGetPr(prhs[3]);
SNR = mxGetPr(prhs[4]);

/* Now take the input block. This is an encoded block and sphere detector will do the transmission too -- I can change it later */
double *block = mxGetPr(prhs[5]);
double *LIST_SIZE = mxGetPr(prhs[6]);

double **cand_sym;
int a = *mod_scheme;
int b = *Mt;
int c = *Mr;
int d = *block_length;
int e = *SNR;
int f = *LIST_SIZE;

int bitSize = (int)(log10(1.0*a)/log10(2.0));

for(int i=0; i<(int)*block_length; ++i)
printf("%d\n", (int)block[i]);
printf("Hello world %d %d %d %d %d!\n", (int)*mod_scheme, (int)*Mt, (int)*Mr, (int)*block_length, (int)*SNR);

/* Inputs are read correctly now set the outputs */
double *llr, *cand_dist;

/* for llrs */
plhs[0] = mxCreateDoubleMatrix(1, d, mxREAL);
llr = mxGetPr(plhs[0]);

/* for cand_dist */
int no_mimo_sym = d/(b*bitSize);
plhs[1] = mxCreateDoubleMatrix(1, f*no_mimo_sym, mxREAL);
cand_dist = mxGetPr(plhs[1]);

/* for cand_syms */
plhs[2] = mxCreateDoubleMatrix(b*bitSize*no_mimo_sym, f,mxREAL); //transposed version
double *candi;
candi = mxGetPr(plhs[2]);

cand_sym = (double**)mxMalloc(f*sizeof(double*));
if(cand_sym != NULL)
for(int i=0;i<f; ++i)
cand_sym[i] = candi + i*b*bitSize*no_mimo_sym;

// mxFree(cand_sym);

The portion inside the sphere decoder code where I get read exception looks like this:

for(int _block_length=0;_block_length<block_length; _block_length+=Mt*bitSize)
        printf("Transmitting MIMO Symbol: %d\n", _block_length/(Mt*bitSize));
        for(int _antenna = 0; _antenna < Mt; ++_antenna)
            for(int _tx_part=0;_tx_part<bitSize; _tx_part++)
                           // PROGRAM CRASHES EXECUTING THIS LINE 
                bitstream[_antenna][_tx_part] = (int)block_data[_block_length + _antenna*bitSize + _tx_part];
      ............................REST OF THE CODE..................

Any help would be appreciated.

With regards, Newbie


Well I finally managed to solve the problem. It was a very stupid mistake that I made. I had a pointer to a pointer(double *a;) of data type double and by mistake I assigned it memory of integer (I ran a find and replace command where I changed lots of int to double but this one left). Hence heap was getting corrupted. Also I changed my Mex function where I created dynamic variables using calloc and passed them to the C++ function. Once C++ function returned I copied there values to matlab variables and freed them usind free().

Need Your Help

Knockout data-bound bootstrap carousel issue

jquery twitter-bootstrap knockout.js twitter-bootstrap-3

I am having difficulties updating/rerendering a bootstrap carousel control data bound to a Knockout observable array (self.Notes). I have greatly simplified the provided code because it was too much

noUiSlider: Adjusting rounded input values

javascript jquery range rangeslider nouislider

When I enter a non-rounded number into the #annual-sales input field, the plugin automatically rounds it up or down, depending on "upper" or "lower".