memory leak in C Mex file
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Francesco
el 11 de Nov. de 2014
Comentada: Francesco
el 12 de Nov. de 2014
Hello,
I wrote a C mex script, and it runs, however, after running it several times, it crashes Matlab, meaning that there is a memory leak somewhere.
This is the first time I tried to pass into the MEX routine a 2D matrix, so it's probably what's causing the problem.
The 2D matlab matrix is called "connectivity"
Within mexFunction I define the following:
connectivity_pointer = (double *) mxGetPr(prhs[9]);
/*create 2D array for connectivity */
connectivity_M = mxGetM(prhs[9]);
connectivity_N = mxGetN(prhs[9]);
connectivity = mxCalloc(connectivity_N,sizeof(double*));
for (j=0;j<connectivity_N;j++){
connectivity[j]=connectivity_pointer+connectivity_M*j;
}
/*do stuff*/
mxFree(connectivity);
return;
Is this correct?
I have no idea what it could otherwise be.. On a side note, I have the new Yosemite, Matlab 2014b, and XCode 5.0, and every time I try to create a MEX debugging session with XCode it crashes (even before attaching processes)! Anyone experienced that?
Francesco
0 comentarios
Respuesta aceptada
Geoff Hayes
el 12 de Nov. de 2014
Francesco - it may be more helpful to include the complete MEX function. Just attach it to your question using the paperclip button (you will have to change the file extension from c to txt in order to attach it successfully). Also include some sample inputs to this function so that we can try it out. More importantly, include the line that fails. What is different about the final function call when compared with the previous ones? And are the previous calls to this function returning valid/expected results?
It seems that your code expects as input a double matrix of dimension connectivity_M by connectivity_N. Based on the lines that follow, are you assuming that this input vector is a row vector (so connectivity_M is one)? Or is it a matrix and you are only interested in a certain part of it? If that is the case, then what are you attempting with
connectivity[j]=connectivity_pointer+connectivity_M*j;
Since connectivity_pointer is a pointer to your input array/matrix, then on each iteration of the loop, you seem to be just adding a number to the address of the first element in the array and saving that address to connectivity.
I suspect that you have declared connectivity as
double* connectivity = 0;
and then have allocated some memory to it with
connectivity = mxCalloc(connectivity_N,sizeof(double*));
But look at the size input - the code is determining the size of a pointer to a double with sizeof(double *) rather than the size of a double as sizeof(double). I think that this may be incorrect, and that your allocation should be
connectivity = mxCalloc(connectivity_N,sizeof(double));
Now, connectivity will be an array of connectivity_N doubles, rather than pointers to doubles. Of course then you will have to change your for loop code to
for (j=0;j<connectivity_N;j++){
connectivity[j]=connectivity_pointer[connectivity_M*j];
}
or something similar.
Más respuestas (0)
Ver también
Categorías
Más información sobre Install Products 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!