User`s manual
1 Calling C and Fortran Programs from MATLAB
1-36
symbols or unresolved references. Be sure to link against the library that
defines the function in question.
On Windows, MATLAB will fail to load MEX-files if it cannot find all DLLs
referenced by the MEX-file; the DLLs must be on the path or in the same
directory as the MEX-file. This is also true for third party DLLs.
Problem 4 - Segmentation Fault or Bus Error
If your MEX-file causes a segmentation violation or bus error, it means that the
MEX-file has attempted to access protected, read-only, or unallocated memory.
Since this is such a general category of programming errors, such problems are
sometimes difficult to track down.
Segmentation violations do not always occur at the same point as the logical
errors that cause them. If a program writes data to an unintended section of
memory, an error may not occur until the program reads and interprets the
corrupted data. Consequently, a segmentation violation or bus error can occur
after the MEX-file finishes executing.
MATLAB provides three features to help you in troubleshooting problems of
this nature. Listed in order of simplicity, they are:
•
Recompile your MEX-file with argument checking (C MEX-files only).
You can add a layer of error checking to your MEX-file by recompiling with
the
mex script flag -argcheck. This warns you about invalid arguments to
both MATLAB MEX-file (
mex) and matrix access (mx) API functions.
Although your MEX-file will not run as efficiently as it can, this switch
detects such errors as passing
null pointers to API functions.
•
Run MATLAB with the -check_malloc option (UNIX only). The MATLAB
startup flag,
-check_malloc, indicates that MATLAB should maintain
additional memory checking information. When memory is freed, MATLAB
checks to make sure that memory just before and just after this memory
remains unwritten and that the memory has not been previously freed.
If an error occurs, MATLAB reports the size of the allocated memory block.
Using this information, you can track down where in your code this memory
was allocated, and proceed accordingly.
Although using this flag prevents MATLAB from running as efficiently as it
can, it detects such errors as writing past the end of a dimensioned array, or
freeing previously freed memory.