Memory erased in mex file
Mostrar comentarios más antiguos
Hi, I'm having a really strange issue with the memory when calling a mex funcion. I have two mex functions, one for initialization and one for processing. the first one looks like this:
#include <matrix.h>
#include <mex.h>
#define NCHANNELS 6
#define SAMPLINGRATE 48000
typedef struct myStruct
{
// Configuration.
int _fsamp; // Sample rate.
int _nchan; // Number of channels.
}myStruct;
void init_function(myStruct *C, int nchan, int fsamp)
{
C->_nchan = nchan;
C->_fsamp = fsamp;
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
int samplingrate;
int nchannels;
myStruct **c_ptr;
plhs[0] = mxCreateNumericMatrix(1, 1, mxINT32_CLASS, mxREAL);
c_ptr = (myStruct **) mxGetData(plhs[0]);
// default values
samplingrate = SAMPLINGRATE;
nchannels = NCHANNELS;
myStruct *c = (myStruct *) mxMalloc(sizeof(myStruct));
//init_function(c, nchannels, samplingrate);
c->_nchan = nchannels;
c->_fsamp = samplingrate;
*c_ptr = c;
mexPrintf("Initialized for %i channels and %i sampling rate.\n",(*c_ptr)->_nchan,(*c_ptr)->_fsamp);
}
and the processing like:
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
int ind, nframe, nchannels;
float *ptr_in[NCHANNELS], *ptr_out[NCHANNELS];
const mwSize outsize[] = {FRAMESIZE, NCHANNELS};
myStruct **c_ptr;
const mxArray *mxInput = prhs[1];
const mxArray *mxPointer = prhs[0];
// Get actual array size
nframe = mxGetM(mxInput); // number of samples per block
nchannels = mxGetN(mxInput);
// create output array
plhs[0] = mxDuplicateArray(mxInput);
// return the pointer
plhs[1] = mxDuplicateArray(mxPointer);
// call processing function
c_ptr = (myStruct **) mxGetData(mxPointer);
mexPrintf("Channels %d, sampling freq. %d\n", (*c_ptr)->_nchan, (*c_ptr)->_fsamp);
if ((*c_ptr)->_nchan != (int) nchannels)
{
mexPrintf("Input signal should have %d channels and sampling freq %d.\n",(*c_ptr)->_nchan, (*c_ptr)->_fsamp);
//mexPrintf("Pointer2 %lld\n", *c_ptr);
return;
}
}
in this simplified version i'm just returning the pointer and the input matrix for the sake of simplicity (the actual code i'm modifiying the input). Let's say i compiled the first mexfunciton as myFunction_init.c and the second as myFunction.c. Still, with this simplified code I get a very strange behaviour when I run this:
data_in = single(randn(512,6));
p = 1;
while p
p = myFunction_init;
for n = 1:10
[data_out,p] = myFunction(p, data_in);
end
end
the first iteration of the while loop gives this:
Initialized for 6 channels and 48000 sampling rate.
Channels 0, sampling freq. 1991125456
Input signal should have 0 channels and sampling freq 1991125456.
which is obviously garbage. The second iteration would return correct data, i.e.:
Initialized for 6 channels and 48000 sampling rate.
Channels 6, sampling freq. 48000
This behaviour happens usually at the first iteration and randomly during the while loop. I still can't figure out what it is happening here, so any help would be really appreciated.
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!