insert new mexPrintf to stop Matlab crash
Show older comments
Hello,
I'm encountering a very weird situation. My Matlab program crashed when I tried to run the mexw64 file and after modifier the c code by adding a mexPrintf("ok") line at the beginning and at the end, the program works. What could happen to my code?
Accepted Answer
More Answers (4)
James Tursa
on 28 Mar 2012
1 vote
Inserting mexPrintf calls changes the state of some internal register(s) and can have an effect on buggy code. E.g., I once saw a case where a function had an incorrect prototype. It really returned a 1-byte value but the prototype said it returned an int, so it was picking up 3 bytes of garbage on the return value. This garbage value was then used downstream for a size and it turned out to be huge and crashed MATLAB. However, by inserting a mexPrintf call just before this mis-prototyped function call things worked. It was because mexPrintf returned a 4-byte int, 3 bytes of which happened to be 0, effectively clearing out the garbage bytes in the return register before the mis-prototyped function call returned its 1 byte value.
Bottom line: Inserting seemingly unrelated printing code can have an effect on the output if your code has errors.
To see what is really going on in your code you will have to post it.
2 Comments
Jane Jean
on 28 Mar 2012
Jan
on 28 Mar 2012
Dear Jane, mexw64 functions are unstable, if the programs contain bugs. Because C-mex functions are standard C-files compiled by standard C-compilers, their level of stability are exactly like all other C-files. This does not concern Matlab.
In opposite to Matlab, it is very easy to create invalid commands with a valid syntax.
Jane Jean
on 28 Mar 2012
3 Comments
James Tursa
on 28 Mar 2012
Haven't looked at this in detail yet, but I will point out right away that if you are linking to the MATLAB blas/lapack libraries you should be using mwSignedIndex for the integer arguments instead of int. E.g.,
void matrixMultiplication(mwSignedIndex rowA, mwSignedIndex colA, mwSignedIndex rowB, mwSignedIndex colB, double *A, double *B, double *C){
/* C = A*B */
mwSignedIndex m = rowA, n = colA, p = rowB, q = colB;
This change may or may not make a difference depending on your system, 32-bit vs 64-bit, -largeArrayDims flag, etc.
Jane Jean
on 29 Mar 2012
Jane Jean
on 29 Mar 2012
Jane Jean
on 29 Mar 2012
Jan
on 29 Mar 2012
0 votes
Does it still crash, if you comment out the dgemm call? You can allocate C much larger than required using mxCalloc and check if any non-zeros appear after the multiplication. Perhaps you confused the leading and trailing dimensions of one of the matrices. The Matlab->C->Fortran convetion conversion is not trivial, although it actually is.
1 Comment
Jane Jean
on 29 Mar 2012
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!