Is there a maximum input size for mex? Segmentation Fault with large inputs
Mostrar comentarios más antiguos
Hi,
I have been developing an application in which i need to read then send large strings from matlab to mex. I cannot split up the string.
I found it was crashing occasionally with a segmentation fault and i have been able to replicate it ( reasonably reliably - it does not crash everytime ) with the mex script below:
/****************************************************************
This function is testing the reading in a string in mex
***************************************************************/
#include "mex.h"
#include "matrix.h"
#include <signal.h>
#include <setjmp.h>
#include <stdlib.h>
#include <gpib/ib.h>
#include <string.h>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
/* INPUTS AND OUTPUT */
int ud = mxGetScalar(prhs[0]);
char * array = mxArrayToString(prhs[1]);
int len = mxGetScalar(prhs[2])+1;
int * ret;
plhs[0] = mxCreateNumericMatrix(1, 1, mxINT32_CLASS, mxREAL);
ret = (int*)mxGetPr(plhs[0]);
ret[0]= -1;
mexPrintf("Length = %i\n",len);
char * buffer = mxMalloc(len);
if(buffer == NULL || array == NULL){
ret[0] = -2;
return;
}
int i;
mexPrintf("Test 0\n");
for(i=0; i<(len-1); i++){
buffer[i] = array[i];
/* CRASH IN HERE */
}
mexPrintf("Test 1\n");
buffer[len-1] = '\0';
mexPrintf("Test 2\n");
mexPrintf("%s\n",buffer);
mxFree(buffer);
return;
}
I compiled with mex (gcc-4.3) and the -g flag and I tested it with the following code:
input = char(mod(1:1500000,256));
mex_test(16,input,length(input))
The output for a crash is:
Test 0
***CRASH***
I have a work around of writing the data to a file using matlab functions and then reading from it in the c file. This seems to be fine with the large strings.
Btw, for my application the length wouldn't be above 1.5e6 characters
I don't think there are any errors in my mex code as it seems to be fine for smaller arrays, but if there are any suggestions i would be grateful
Thanks, Stephen
1 comentario
James Tursa
el 22 de En. de 2013
I would advise against using "input" as the name of a variable, since there is a built-in MATLAB function of that same name.
Respuesta aceptada
Más respuestas (1)
James Tursa
el 22 de En. de 2013
Editada: James Tursa
el 22 de En. de 2013
What is the connection between len and the size of prhs[1]? How do we know this isn't simply a user input error? I would suggest more robust checking code. E.g.,
char * array = mxArrayToString(prhs[1]);
int len = mxGetNumberOfElements(prhs[1])+1;
Btw, in a mex routine out-of-memory errors never return control back to your program with a NULL pointer ... they simply abort back to the calling routine. Thus the following if-test will never be true:
if(buffer == NULL || array == NULL){
If your strings are so large, why are you printing out buffer to the screen?
Finally, you don't need special code to put the '\0' character at the end of buffer ... mxArrayToString will put one at the end of array for you automatically, so you can simply copy that character as part of your loop and delete the special code entirely (unless it is there for some other purpose that is not shown).
2 comentarios
Stephen
el 22 de En. de 2013
James Tursa
el 22 de En. de 2013
P.S. You are leaking the memory behind array. In this case it is only a temporary leak since array is on the garbage collection list.
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!