Thread Subject: Segmentation violation due to MEX code

Subject: Segmentation violation due to MEX code

From: Jose Antonio

Date: 6 Jul, 2009 12:43:24

Message: 1 of 4

Hi again


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)


Any clue would be appreciated :)

Thanks in advance
Jose

Subject: Segmentation violation due to MEX code

From: Jose Antonio

Date: 6 Jul, 2009 13:34:17

Message: 2 of 4

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 4000 values and it performs correctly...

Any idea if what I say makes sense?

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

Thanks!

Subject: Segmentation violation due to MEX code

From: James Tursa

Date: 6 Jul, 2009 19:52:02

Message: 3 of 4

Jose Antonio <juriguen@gmail.com> wrote in message <28112995.79623.1246887287751.JavaMail.jakarta@nitrogen.mathforum.org>...
> 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 4000 values and it performs correctly...
>
> Any idea if what I say makes sense?
>
> If so, how can I increase the memory assigned to the MEX function?
>
> Thanks!

Compiled and ran without seg fault a dozen times with random data using your specified sizes. So I can't reproduce your error. Are all of your inputs double class?

James Tursa

Subject: Segmentation violation due to MEX code

From: Jose Antonio

Date: 6 Jul, 2009 20:17:02

Message: 4 of 4

Woah

It was that! Thanks so much.

I actually was using y being a matrix of integer values, since it is stored like that when reading an image (uint8)

What would be best to simply pass double(y) to the function or try to read them as integers?

In fact, I have modified the C code, and right now I use the y[index] values as indices for performing operations, rather than looping once and again for (m ...) and scanning all the data for (x...) for (y...)

Jose

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com