mex fcn makes trouble ...

2 views (last 30 days)
Ulrich
Ulrich on 13 Nov 2014
Commented: Titus Edelhofer on 13 Nov 2014
Well, hello dear experts, i encountered the following phenomenon:
I wrote and compiled (and reviewed in debug mode) a mex function, that is quite simple and only duplicates values from the input to an array that is written to the output. I declared my output with "mxCreateDoubleArray", the function ends fine in the debugger, but when it comes to "reenter" the RTE of matlab, the program (MATLAB) crashes ... well, it seems to be "indefinite" in so far, as Matlab seems to roll the dice whether to crash or not ... see below.
#include "mex.h"
#define TIMES_IN ((double*)mxGetPr(pin[0]))
#define VALUES_IN ((double*)mxGetPr(pin[1]))
void mexFunction(int nout, mxArray* pout[], int nin, const mxArray* pin[]){
int ind = 1;
size_t FLAG = 0;
size_t len_1 = 0;
size_t len_2 = 0;
double* times;
double* values;
// all fine
if (nin != 2){
// error
mexErrMsgIdAndTxt( "MATLAB:mexfunction:arg error",
"2 inputs required.\n");
}
if( mxGetN(pin[0]) > mxGetM(pin[0])){
len_1 = mxGetN(pin[0]);
FLAG = 1;
}
else{
len_1 = mxGetM(pin[0]); // rows ...
FLAG = 0;
}
if( mxGetN(pin[1]) > mxGetM(pin[1])){
len_2 = mxGetN(pin[1]);
}
else{
len_2 = mxGetM(pin[1]);
}
if(nout != 2){
mexErrMsgIdAndTxt( "MATLAB:mexfunction:arg error",
"2 outputs required.\n");
}
// rep. vectors ...
if(len_1 != len_2){
mexErrMsgIdAndTxt( "MATLAB:mexfunction:arg error",
"inputsize not matching requirements ... \n");
}
pout[0] = mxCreateDoubleMatrix((mwSize)(2*len_1 - 1), (mwSize)1, mxREAL);
pout[1] = mxCreateDoubleMatrix((mwSize)(2*len_1 - 1), (mwSize)1, mxREAL);
// better: resize the outputs ... ??
values = (double*)mxGetPr(pout[0]);
times = (double*)mxGetPr(pout[1]);
values[0] = (double)(VALUES_IN[0]);
times[0] = (double)(TIMES_IN[0]);
while(ind < len_1){
times[2*ind -1] = (double)(TIMES_IN[ind]);
values[2*ind -1] = (double)(VALUES_IN[ind]);
times[2*ind] = (double)(TIMES_IN[ind]);
values[2*ind] = (double)(VALUES_IN[ind]);
ind ++;
}
times[2*len_1 -1] = (double)(TIMES_IN[len_1 -1]);
values[2*len_1-1] = (double)(VALUES_IN[len_1 -1]);
//
}
... so, this mex file causes Matlab to crash ... not always, but from time to time ... the first time i call the function ALWAYS worked however (also when looped 10000 times) but a second call (looped or not looped)... or sometimes a third one was the death of the Matlab process. However, when i cleared the outputs from Matlab ws, it worked fine and as expected ... when i leave the outputs untouched, with a high probability Matlab crashes on the second call ...
It got even better ... it worked for session, then i restarted Matlab and it crashed when trying to call the function ...
So, what am i doing wrong here ?? I wrote mex Functions before, but it is a while ago, and i never encountered such a behaviour. Usually my bugs were reliable :-)
best regards, thanks for reading this post and thanks for any hints,
....

Accepted Answer

Titus Edelhofer
Titus Edelhofer on 13 Nov 2014
Hi,
if I see it correctly your result arrays are too small:
pout[0] = mxCreateDoubleMatrix((mwSize)(2*len_1 - 1), (mwSize)1, mxREAL);
values = (double*)mxGetPr(pout[0]);
means that you can index in values from 0,...,2*len_1-2. But you access values[2*len_1-1] after the loop ... Probably changing the mxCreateDoubleMatrix call to (2*len_1) should do the trick.
Titus

More Answers (2)

Ulrich
Ulrich on 13 Nov 2014
... well, yes, that seems to be the case ... index but that should lead to a reproduceable crash, or not ?
I'll fix this this and try again.
best regards and thanks a lot so far,
  1 Comment
Titus Edelhofer
Titus Edelhofer on 13 Nov 2014
Hi Ulrich,
that's why debugging C code is often not that easy: it's neither reproducible, and most often the crash does not happen where the problem is. Assume you have in memory the variables in the order values, times, len_1. Going out of the boundaries of values and times will not produce an error. But len_1 will be overwritten with false values ...
Titus

Sign in to comment.


Ulrich
Ulrich on 13 Nov 2014
... and it does solve the issue.
Sorry, but i was sure that in case of such sort of programming error, the application crashes immediately ... and with an error message telling me which mex-dll was the source of the desaster. I was very confused by the "unexpected" behaviour of my Matlab ...
Thanks, again,
...

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!