我编了一个mex文件调用c程序,这个c程序是可以运行的,应该没有什么错误,但在运行matlab时也出现了
Segmentation violation detected.... This error was detected while a MEX-file was running. 
这样的错误,我不知道是怎么回事。
敢问您的问题解决了吗?如解决,请赐教!

解决方案 »

  1.   

    segment error一般都是数组越界等问题,当你使用大数组的时候,看是否有越界操作,以及堆栈溢出,数组放到堆上
      

  2.   

    首先你得仔细检查你的程序,有没有越界调用情况,然后一步步调试检查。
    我出现这个问题是当数组维数过大时才出现的。
    估计是matlab和VC关于内存动态分配的差异造成的。但还没仔细研究过。
      

  3.   

    我也遇到过这种问题,具体原因尚不清楚。有以下显著现象:
    换一个环境就可以运行了,VC 6.0+WINDOWS2003+ matlab (R2007a)下可以运行;VC 2008 + WIN7 HOME BASIC + matlab (R20009a)出错。结论: 是配置环境的问题,具体不详,编写的mex程序适应性差,对环境有依赖。
      

  4.   

    我也遇到一个类似问题,是内存分配问题。
    贴一下官网的解释:
    As noted in the segmentation violation crash dump:
    "This error was detected while a MEX-file was running. If the MEX-file is not an official MathWorks function, please examine its source code for errors. Please consult the External Interfaces Guide for information on debugging MEX-files."
    Before MathWorks can provide assistance with this issue, you will need to determine where the problem is within your code by debugging it. Most likely, an attempt has been made to write to unallocated memory, or memory is destroyed that is later used by MATLAB.One debugging technique that may be helpful is to run MATLAB in check_malloc mode. When run in this mode, MATLAB attaches a header and footer to each block of allocated memory. This information can then be used to determine if an attempt is made to write to unallocated memory. Note that, in general, MATLAB will run slower when in check_malloc mode.For more information on running MATLAB in check_malloc mode, see the Related Solution listed below:Additionally, be sure to use mxMalloc in your code for any memory that is dynamically created and be sure not to use mxFree on any data returned to MATLABAnother common error is filling an mxArray in an improper manner. The Related Solution listed below shows four correct methods for filling an mxArray.