Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Mex mxCreateDoubleMatrix() destroys different pointer sporadically

Subject: Mex mxCreateDoubleMatrix() destroys different pointer sporadically

From: Max

Date: 7 Jan, 2013 17:57:07

Message: 1 of 5

I keep running in to segmentation faults when running this Mex file, but it is sporadic and cannot be predicted. The problem comes from the following bit of code:

for (j =0; j<t->neighbors; j++) {
...
ccv_contour_t* cont = t->letters[j]->contour;
mxArray* S = mxCreateDoubleMatrix(cont->size, 1, mxREAL); //THIS IS THE PROBLEM LINE
}

Leaving out the mxArray* line, everything works perfectly, however when adding in the line causes the variable cont to sometimes be NULL for some iterations of the loop. I'd say this behaviour happens around 50% of the times I run the exact same code.

It must be something to do with the memory allocated, but I can't figure out what is wrong (and I have spent hours on this now...)

Subject: Mex mxCreateDoubleMatrix() destroys different pointer sporadically

From: James Tursa

Date: 7 Jan, 2013 18:52:12

Message: 2 of 5

"Max " <maxjaderberg@gmail.com> wrote in message <kcf29j$l93$1@newscl01ah.mathworks.com>...
> I keep running in to segmentation faults when running this Mex file, but it is sporadic and cannot be predicted. The problem comes from the following bit of code:
>
> for (j =0; j<t->neighbors; j++) {
> ...
> ccv_contour_t* cont = t->letters[j]->contour;
> mxArray* S = mxCreateDoubleMatrix(cont->size, 1, mxREAL); //THIS IS THE PROBLEM LINE
> }
>
> Leaving out the mxArray* line, everything works perfectly, however when adding in the line causes the variable cont to sometimes be NULL for some iterations of the loop. I'd say this behaviour happens around 50% of the times I run the exact same code.
>
> It must be something to do with the memory allocated, but I can't figure out what is wrong (and I have spent hours on this now...)

Unable to provide any meaningful comments unless we see your code. The small snippet above is pretty much useless to us.

James Tursa

Subject: Mex mxCreateDoubleMatrix() destroys different pointer sporadically

From: Max

Date: 7 Jan, 2013 20:49:15

Message: 3 of 5

Here is the full code https://gist.github.com/4478220

Basically it seems that when allocating a new large mxArray* S (doesnt matter whether it is a matrix or cell) it seems that it interfers with the memory already assigend to other variables. Also the memory given to S sometimes cannot be written too. So there are two symptoms of this problem,
1. the variable ccv_contour_t* cont is set to zero somehow
2. the memory assigned by S = mxCreateDoubleMatrix(cont_n, 1, mxREAL) cannot be written too (but can be read).

I must stress that this problem occurs around 50% of runs, even though there is no randomness in the code.

Subject: Mex mxCreateDoubleMatrix() destroys different pointer sporadically

From: Max

Date: 7 Jan, 2013 20:52:08

Message: 4 of 5

Here is the full code https://gist.github.com/4478220

Basically it seems that when allocating a new large mxArray* S (doesnt matter whether it is a matrix or cell) it seems that it interfers with the memory already assigend to other variables. Also the memory given to S sometimes cannot be written too. So there are two symptoms of this problem,
1. the variable ccv_contour_t* cont is set to zero somehow
2. the memory assigned by S = mxCreateDoubleMatrix(cont_n, 1, mxREAL) cannot be written too (but can be read).

I must stress that this problem occurs around 50% of runs, even though there is no randomness in the code.

Subject: Mex mxCreateDoubleMatrix() destroys different pointer sporadically

From: Max

Date: 8 Jan, 2013 12:34:10

Message: 5 of 5

Sorry for the double reply.

I have solved this problem:

I was using a third party library that does not allocate memory using mxMalloc so the memory was not registered with matlab so conflicted with mx memory. To solve the issue I preallocated memory with matlab (mxMalloc) of more than I knew I needed before using the third party library functions, which has seemed to work.

Tags for this Thread

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.

Contact us