问题
在使用matlab的csfunction编程时出现了错误:matlab调用.mex函数时,第一次调用正常,第二次调用出现matlab闪退、或者卡死无反应的错误,然后崩溃信息提示堆损坏。然而matlab没有如vs那样方便的编辑器和调试器,因此在找错误的时候非常困难。
问题分析
- 猜想可能是数组越界等导致的内存问题,然后在网上找到这样一篇文章:有关奇葩的mex编程时的matlab出现栈内存错误的问题,大致分析了出错的原因。
- matlab提供的调试器功能有限,可以借助第三方软件进行调试,matlab的官方文档内介绍了有关debug C mex S-Functions的技巧和方法。
解决措施
官方文档内介绍:
Debugging C MEX S-Functions Using the Microsoft Visual C++ .NET Environmen Before beginning the example, save the files sfcndemo_timestwo and timestwo.c into your working folder.
- Open the Simulink model sfcndemo_timestwo.c
- Create a version of the MEX file that you can debug by compiling the C file using the mex command with the -g option.
mex -g timestwo.c
The -g option creates the executable timestwo.mexw32 with debugging symbol