Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Segmentation violation due to MEX code
Date: Mon, 6 Jul 2009 19:49:01 +0000 (UTC)
Organization: Imperial College London
Lines: 130
Message-ID: <h2tkfd$l84$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1246909741 21764 172.30.248.38 (6 Jul 2009 19:49:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 6 Jul 2009 19:49:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1570235
Xref: news.mathworks.com comp.soft-sys.matlab:553194


Hi!


I am trying to implement a very simple subroutine embeding a CMEX code in Matlab.

Does anybody know if it is possible that I get a "Segmentation violation" error even if I don't go beyond the iput arrays' limits?


I have checked the code with small inputs, and works fine. But when I send a rather huge array, it crashes.

This is the code:
#include "mex.h"

/*
* Calculate_I_Values.c
* Used to speed up the I mapping calculations 
*
* This is a MEX-file for MATLAB.
*/


/* The gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
/* variable declarations here */
double *e, *x, *y, *I;
int eRows, eCols, xRows, xCols, yRows, yCols, index, ySize;
int m, i, j;
double num, den;

e = mxGetPr(prhs[0]);
eRows = mxGetN(prhs[0]);
eCols = mxGetM(prhs[0]);

x = mxGetPr(prhs[1]);
xRows = mxGetN(prhs[1]);
xCols = mxGetM(prhs[1]);

y = mxGetPr(prhs[2]);
yRows = mxGetN(prhs[2]);
yCols = mxGetM(prhs[2]);
ySize = mxGetNumberOfElements(prhs[2]);


/* Allocate output memory */
plhs[0] = mxCreateDoubleMatrix(1, 256, mxREAL);
I = mxGetPr(plhs[0]);


/* code here */
printf("The xNumRow is: %d\n", (int)xRows);
printf("The xNumCol is: %d\n\n", (int)xCols);
printf("The yNumRow is: %d\n", (int)yRows);
printf("The yNumCol is: %d\n\n", (int)yCols);
printf("The ySize is: %d\n\n", (int)ySize);
printf("The eNumRow is: %d\n", (int)eRows);
printf("The eNumCol is: %d\n\n", (int)eCols);

for (m = 0; m <= 255; m++)
{
printf("m = %d -> ", (int)m);
num = 0;
den = 0;
for (i = 0; i < yRows; i++)
for (j = 0; j < yCols; j++)
{
index = i * yCols + j;
if (index >= ySize)
printf("Wrong\n");
else if (m == y[index])
{
num += e[j] * x[i];
den++;
}
}
if (den == 0) I[m] = num;
else I[m] = num / den;
}

}


It is actually pretty simple, but...

I call the function by:
Calculate_I_Values(exposures.', x.', y.');

Obviously after compiling.


The first printf lines give me:
The xNumRow is: 361862
The xNumCol is: 1

The yNumRow is: 361862
The yNumCol is: 16

The ySize is: 5789792

The eNumRow is: 1
The eNumCol is: 16

So everyting is as it should be. But then I get the segmentation violation error.


I have commented different parts of the program, and concluded that the error is when I do y[index]. So I guess I am trying to access a memory address which is shouldn't...

But I have already controlled that the index does not exceed the size of the input array! (ySize)


Anyway, if I don't let that line to run, and I print the index, i and j values, after the double loop ends, I get:
index: 5789791; i: 361862; j: 16

Which are fine! (i and j are 1 over the limit, so that's why the double loop has ended, and index is also correct)


I am pretty sure the problem has to do with memory allocation when calling the function.

I think so because I have tried to make it work with arrays with up to 10000 values and it performs correctly... I actually need to handle a 700x600x16 pixels array, so I am far from that!

Any idea if what I say makes sense?

If so, how can I increase the memory assigned to the MEX function?


Any clue would be appreciated :)

Thanks in advance
Jose