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.

 Accepted Answer

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.

More Answers (6)

Jan
Jan on 15 Dec 2011
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 on 15 Dec 2011
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!.
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.
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 Comment

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

Sign in to comment.

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

Categories

Community Treasure Hunt

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

Start Hunting!