Why do I receive a segmentation violation when I run a C-MEX file or Fortran MEX file that I compiled?

91 views (last 30 days)
I have created a C-MEX file or Fortran MEX file that compiles successfully using the MEX command. When I run this MEX-function from the MATLAB command prompt, MATLAB issues a segmentation violation and stack trace.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 18 Apr 2023
Edited: MathWorks Support Team on 19 Apr 2023
As noted in the segmentation violation crash dump:
ERROR: "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 MATLAB. See the following URLs for more information on these functions:
mxMalloc (C and Fortran) :: C and Fortran Functions -
Alphabetical List (MATLAB C and Fortran API Reference)
mxFree (C and Fortran) :: C and Fortran Functions -
Alphabetical List (MATLAB C and Fortran API Reference)
Another common error is filling an mxArray in an improper manner. The Related Solution listed below shows four correct methods for filling an mxArray.
Another helpful debugging technique is to include mexPrintf functions periodically in your code. This technique can ultimately be used to determine the line of code that is generating the segmentation violation. By examining the history of pointers used on the offending line, one can determine where they went awry.
For documented MEX debugging tips, see the following External Interfaces documentation URL:
For additional information on debugging C MEX files, consult MathWorks technical support website at the following URL:
At the right side of this page in the "Search" area, select "Solutions and Technical notes". In the search text box, enter "debugging mex" and click the "go" button.
This search may produce a list of any additional solutions providing further information on debugging MEX-files, particularly within certain IDEs.

More Answers (0)

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!