Mex file crash on Windows 64bit environment

Hi,
I am developing a piece of code inside a mex function. I output interface is something like this
Output = mxCreateNumericMatrix(SIZE, 1,mxLOGICAL_CLASS,mxREAL);
dec = (bool*)mxGetData(Output);
After this, i dynamically allocate memory to an other entity
if (( Table =
(uint16_t*)mxMalloc(sizeof(uint16_t) * SIZE)) == NULL)
{
}
Now, When i print the address's of Table and also dec, i find that they overlap.
Any idea on what could be happening? Is this an issue with windows 64 bit environment?.
At the end, while freeing the Table pointer there is a crash.
but the same code works fine on 32bit machine.
Thanks, Kishore.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 15 Dez. 2011

0 Stimmen

Which compiler are you using?
On some compilers, bool can be 4 bytes rather than 1 byte. See for example http://msdn.microsoft.com/en-us/library/tf4dy80a.aspx
I also find record of a compiler for which bool was 8 bytes.

Weitere Antworten (6)

Jan
Jan am 15 Dez. 2011

1 Stimme

Please post more code. The cause of the crash is somewhere else. Of course there is no general problem with 64 bit systems, which would create overlapping memory blocks.
Are you sure that mxLogical has the same size as bool? I'd stay at mxLogical for dec.
How do you "print the address"? Do you use "%i" or "%li" as format?
Kishore
Kishore am 15 Dez. 2011

0 Stimmen

Hi Jan, Thanks for the reply.
I used %x format specifier to print the address. I will examine my code again today before posting.
Thanks!.
Kishore
Kishore am 15 Dez. 2011

0 Stimmen

Ok, is there anyway i can determine the total size of the allocated memory?
For example %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Data_o = mxCreateNumericMatrix(CodeBlockSize, 1, mxLOGICAL_CLASS, mxREAL);
dec = (mxLogical*)mxGetData(Data_o);
or
dec = (bool*)mxGetData(Data_o);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Here, Data_o is a mxArray*, how can i know how much size was allocated?.This might give me a clue on what is going on,
The crash is occuring when trying to write to a pointer of this type. It is supposed to be of length 5740 * 4 bytes.
Interestingly, no crash happens till i write to 1440*4 bytes to dec.
Thanks, Kishore.
Kishore
Kishore am 15 Dez. 2011

0 Stimmen

Ok, i created a simple .c file to isolate the issue
#include <math.h>
#include <mex.h>
#define Data_o plhs[1]
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
// Input variables
mxLogical* HardDecisions_b;
int CodeBlockSize = 5760;
int i = 0;
Data_o = mxCreateNumericMatrix(CodeBlockSize,1,mxLOGICAL_CLASS, mxREAL);
if( Data_o != NULL)
HardDecisions_b = (mxLogical*)mxGetData(Data_o);
for (i=0; i<CodeBlockSize; i++)
{
HardDecisions_b[i] = true;
}
mxDestroyArray(Data_o);
}
1) Here, if i comment out the section
for (i=0; i<CodeBlockSize; i++)
{
HardDecisions_b[i] = true;
},,there is no crash,
2) Also, if i make the incraese the size by 4 times,
Data_o = mxCreateNumericMatrix(CodeBlockSize*4,1,mxLOGICAL_CLASS, mxREAL);
there is no crash.
Crash occurs only when i try to write to HardDecisions_b, and around index 3888 .
Thanks, Kishore.

1 Kommentar

Jan
Jan am 15 Dez. 2011
I do not get problems with this code (Matlab 2009a/64/Win7/MSVC2008)
Nevertheless, mxCreateNumericMatrix is not specified for mxLogical. Try to use mxCreateLogicalMatrix.

Melden Sie sich an, um zu kommentieren.

Kishore
Kishore am 15 Dez. 2011

0 Stimmen

hmm.,i am using MS Studio 2010.
Kishore
Kishore am 15 Dez. 2011

0 Stimmen

Thanks all for the answers.
Well the issue was bool was 1 byte and my platform file defined it as 4 bytes!.

Kategorien

Mehr zu Write C Functions Callable from MATLAB (MEX Files) finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by