Got Questions? Get Answers.
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:
Matlab crashes from MEX Function

Subject: Matlab crashes from MEX Function

From: Ryan

Date: 10 Aug, 2012 15:35:19

Message: 1 of 4

Hi, I have a large program written in c++ that I have been writing a MEX function for, to interface with matlab. I am passing both vectors and single values from Matlab. My c++ program will return two arrays of data to Matlab, with lengths depending on an input parameter. When I call my function from Matlab, It will run and return the first value in both of the output arrays, and then crashes Matlab, and I am even unable to reopen matlab afterwards without a restart. My code looks like this:

mexFunction(...,...){

  m = mxGetM(no_of_carriers_in);
    n = mxGetN(no_of_carriers_in);

    // /* Assign pointers to the various parameters */

 /* Ouputs*/
  /* Create a matrix for the return argument */
 data1_mat_out = mxCreateDoubleMatrix( (mwSize)m, (mwSize)n, mxREAL);
    data1_out_ptr = mxGetPr(data1_mat_out);
len6=mxGetM(data1_mat_out);
vector<double> data1_out_data(data1_out_ptr,(data1_out_ptr+len6));

 
  /* Create a matrix for the return argument */
 data2_mat_out = mxCreateDoubleMatrix( (mwSize)m, (mwSize)n, mxREAL);
data2_out_ptr = mxGetPr(data2_mat_out);
len7=mxGetM(data2_mat_out);
vector<double> data2_out_data(data2_out_ptr,(data2_out_ptr+len7));

/*inputs*/
double *beta_ptr = (double *)mxGetData(beta_in);
len2=mxGetM(beta_in);
vector<double> beta_data(beta_ptr,(beta_ptr+len2));

int *a_ptr =(int *)mxGetData(prhs[0]);
double data5=*a_ptr;
int a_data=data5;

foo(a_data,beta_data,data1_data_out,data2_data_out);
}
//c++ code

foo(a_data, beta,vector<double> &data1_out, vector<double>&data2_out){
.....
....
....
}

With the same three lines used for a_data and beta, repeated for 5 other different variables and vectors.
Those three lines are supposed to recast the data coming from matlab to an integer or whatever type I need it to even if it comes as a double.

Subject: Matlab crashes from MEX Function

From: Ryan

Date: 10 Aug, 2012 15:45:20

Message: 2 of 4

Also, my log file says Matlab suffered from an Exception_Access_Violation

Subject: Matlab crashes from MEX Function

From: Bruno Luong

Date: 10 Aug, 2012 18:14:11

Message: 3 of 4

"Ryan " <surfboard3r@aol.com> wrote in message <k03aag$shf$1@newscl01ah.mathworks.com>...
> Also, my log file says Matlab suffered from an Exception_Access_Violation

It sounds like a typically memory and pointer problems. Impossible to guess with the snippet you post, the problem might be down stream.

Also the back and forth casting

[ nt *a_ptr =(int *)mxGetData(prhs[0]);
double data5=*a_ptr;
int a_data=data5; ]

is bizarre. Why doing like that? Also danger there, you should check the class of prhs[0] before assuming it contains integers.

Are you sure about using C++ vector to encapsulate the data? It seems the vector constructor as you did will copy the data. It's not good (for the outputs). In any case I rather use regular C pointers rather than C++ vectors.

All that trouble to avoid passing the size of the arrays to foo()?

Bruno

Subject: Matlab crashes from MEX Function

From: James Tursa

Date: 10 Aug, 2012 20:41:16

Message: 4 of 4

"Ryan " <surfboard3r@aol.com> wrote in message <k039nn$qdd$1@newscl01ah.mathworks.com>...
>
> /* Ouputs*/
> /* Create a matrix for the return argument */
> data1_mat_out = mxCreateDoubleMatrix( (mwSize)m, (mwSize)n, mxREAL);
> data1_out_ptr = mxGetPr(data1_mat_out);
> len6=mxGetM(data1_mat_out);
> vector<double> data1_out_data(data1_out_ptr,(data1_out_ptr+len6));
>
>
> /* Create a matrix for the return argument */
> data2_mat_out = mxCreateDoubleMatrix( (mwSize)m, (mwSize)n, mxREAL);
> data2_out_ptr = mxGetPr(data2_mat_out);
> len7=mxGetM(data2_mat_out);
> vector<double> data2_out_data(data2_out_ptr,(data2_out_ptr+len7));
>

As Bruno points out, your vector initialization above will not get downstream results copied into the MATLAB output variables.

> /*inputs*/
> double *beta_ptr = (double *)mxGetData(beta_in);
> len2=mxGetM(beta_in);
> vector<double> beta_data(beta_ptr,(beta_ptr+len2));
>
> int *a_ptr =(int *)mxGetData(prhs[0]);
> double data5=*a_ptr;
> int a_data=data5;
>
> Those three lines are supposed to recast the data coming from matlab to an integer or whatever type I need it to even if it comes as a double.

Then use mxGetScalar(prhs[0]) instead of these three lines. The above lines will only work if the input is an int32 or uint32 class variable.

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