insert new mexPrintf to stop Matlab crash
古いコメントを表示
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?
採用された回答
その他の回答 (4 件)
James Tursa
2012 年 3 月 28 日
1 投票
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 件のコメント
Jane Jean
2012 年 3 月 28 日
Jan
2012 年 3 月 28 日
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
2012 年 3 月 28 日
3 件のコメント
James Tursa
2012 年 3 月 28 日
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
2012 年 3 月 29 日
Jane Jean
2012 年 3 月 29 日
Jane Jean
2012 年 3 月 29 日
Jan
2012 年 3 月 29 日
0 投票
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.
カテゴリ
ヘルプ センター および File Exchange で Write C Functions Callable from MATLAB (MEX Files) についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!