调用mxCreateDoubleMatrix()时会出现什么错误?

时间:2011-04-01 12:47:26

标签: c matlab valgrind

我正在用matlab链接来编写我的C代码中的matfiles 运行以下代码时,我得到一个段错误(在第一行):

pPressure_a = mxCreateDoubleMatrix((MWSIZE)2, (MWSIZE)dim, mxREAL);
if(pPressure_a == NULL){
    fatal("Memory alocation error.");
}
copyDoubleToPtr2D(temp2D_a, mxGetPr(pPressure_a), dim, 2);
matPutVariable(matfile, "p", pPressure_a);
mxDestroyArray(pPressure_a);

在上面的代码片段中,“MWSIZE”被#defined定义为int32_t,因为我正在编译这个 使用matlab R14,但#define允许更改这个以便使用 其他版本。 使用gdb,我已经确认当第一行被称为pPressure_a = 为0x0;和dim = 501,这是完全有效的值。

这是回溯:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff73eeb5d in ?? () from /lib/libc.so.6

(gdb) bt
#0  0x00007ffff73eeb5d in ?? () from /lib/libc.so.6
#1  0x00007ffff73f103e in malloc () from /lib/libc.so.6
#2  0x00007ffff7730e35 in ?? () from /usr/local/matlabr14/bin/glnxa64/libut.so
#3  0x00007ffff7731600 in ?? () from /usr/local/matlabr14/bin/glnxa64/libut.so
#4  0x00007ffff7730046 in utCalloc () from /usr/local/matlabr14/bin/glnxa64/libut.so
#5  0x00007ffff7236c76 in mxCreateNumericMatrix () from /usr/local/matlabr14/bin/glnxa64/libmx.so
#6  0x00007ffff7237620 in mxCreateDoubleMatrix () from /usr/local/matlabr14/bin/glnxa64/libmx.so
#7  0x0000000000412c28 in calcCohAcoustPress (settings=0x646ab0) at /home/eey/models/cTraceo/./calcCohAcoustPress.c:324
#8  0x0000000000413736 in main (argc=2, argv=0x7fffffffe038) at cTraceo.c:137

(gdb) f 7
#7  0x0000000000412c28 in calcCohAcoustPress (settings=0x646ab0) at /home/eey/models/cTraceo/./calcCohAcoustPress.c:324
324            pPressure_a = mxCreateDoubleMatrix((MWSIZE)2, (MWSIZE)dim, mxREAL);

我在代码前面以相同的方式(几次)调用mxCreateDoubleMatrix(),并且从未遇到过问题。

导致这种情况的原因是什么?

编辑:运行valgrind产生如下几个块:

==17538== Conditional jump or move depends on uninitialised value(s)
==17538==    at 0x4E560B8: csqrt (s_csqrt.c:66)
==17538==    by 0x40E149: solveDynamicEq (in cTraceo/bin/cTraceo-64b.bin)
==17538==    by 0x411BA5: calcCohAcoustPress (in cTraceo/bin/cTraceo-64b.bin)
==17538==    by 0x413555: main (in cTraceo/bin/cTraceo-64b.bin)

结束于此:

Invalid read of size 8
==17538==    at 0x412A3F: calcCohAcoustPress (in cTraceo/bin/cTraceo-64b.bin)
==17538==    by 0x413555: main (in cTraceo/bin/cTraceo-64b.bin)
==17538==  Address 0x86a3050 is 0 bytes after a block of size 16 alloc'd
==17538==    at 0x4C2815C: malloc (vg_replace_malloc.c:236)
==17538==    by 0x401781: mallocDouble2D (in cTraceo/bin/cTraceo-64b.bin)
==17538==    by 0x41296D: calcCohAcoustPress (in cTraceo/bin/cTraceo-64b.bin)
==17538==    by 0x413555: main (in cTraceo/bin/cTraceo-64b.bin)
==17538== 
==17538== Invalid write of size 8
==17538==    at 0x412A43: calcCohAcoustPress (in cTraceo/bin/cTraceo-64b.bin)
==17538==    by 0x413555: main (in cTraceo/bin/cTraceo-64b.bin)
==17538==  Address 0x0 is not stack'd, malloc'd or (recently) free'd

==17538== Process terminating with default action of signal 11 (SIGSEGV)
==17538==  Access not within mapped region at address 0x0
==17538==    at 0x412A43: calcCohAcoustPress (in cTraceo/bin/cTraceo-64b.bin)
==17538==    by 0x413555: main (in cTraceo/bin/cTraceo-64b.bin)
==17538==  If you believe this happened as a result of a stack
==17538==  overflow in your program's main thread (unlikely but
==17538==  possible), you can try to increase the size of the
==17538==  main thread stack using the --main-stacksize= flag.
==17538==  The main thread stack size used in this run was 8388608.
==17538== Invalid free() / delete / delete[]
==17538==    at 0x4C27D71: free (vg_replace_malloc.c:366)
==17538==    by 0x58FAA0A: free_mem (in /lib/libc-2.12.1.so)
==17538==    by 0x58FA5A1: __libc_freeres (in /lib/libc-2.12.1.so)
==17538==    by 0x4A2366B: _vgnU_freeres (vg_preloaded.c:62)
==17538==    by 0x7FEFFFD27: ???
==17538==    by 0x413555: main (in cTraceo/bin/cTraceo-64b.bin)
==17538==  Address 0x41508a0 is not stack'd, malloc'd or (recently) free'd

可悲的是,我是valgrind的新手并且实际上并不知道这意味着什么......:p

2 个答案:

答案 0 :(得分:1)

实际崩溃是否可能不是在mxCreateDoubleMatrix()调用时发生,而是在copyDoubleToPtr2D()调用时发生?如果是这样,这可能是由于MEX提到here的内部[非常烦人且难以跟踪]限制。解决方案是简单地使用memcpy()而不是copyDoubleToPtr2D()。

答案 1 :(得分:0)

根据mxCreateDoubleMatrix()手册页...“当没有足够的可用堆空间来创建mxArray时,该函数不成功。”

为gcc -Wl尝试这些标志, - heap = large_enough_value