Newsletters - MATLAB News & Notes
Segmentation Violations
Dealing with a troublesome error
by Rob Monteiro
In
this edition of News & Notes , we focus on segmentation violations.
Segmentation violations can occur for a variety of reasons and in many different
situations. For instance, they can occur when you are running MEX files,
M-files, or user-written code in MATLAB. This article explains how to avoid
segmentation violations, what to do when you see one, and why the error message
asks you to contact The MathWorks.
What is a segmentation violation?
A segmentation violation is an error that occurs when MATLAB attempts to write to a region of memory to which it does not have access. This usually happens if the source code either writes past the end of an array or dereferences an uninitialized pointer. In the first case, MATLAB only has legal access to the memory regions within the array; the regions past the end of the array may belong to another program or to part of the current program's executable code. In the latter case, the pointer almost invariably points to a memory region owned by another process.

What can I do
If your MEX-file causes a segmentation violation, it means that the MEX-file has attempted to access protected, read-only, or unallocated memory. Since this is a very general category of programming errors, such errors are difficult to track down. This is because the error message may not be occurring in the part of the code that's causing the error. If a program writes data to an unintended section of memory, the segmentation violation 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.
For CMEX-files: If running a CMEX-file causes a segmentation violation, general protection fault, or bus error, make sure that you have used pointers correctly. That is, verify that you have initialized the pointers correctly, that you did not index any arrays out of bounds, and that you allocated memory where necessary. (please refer to a C book and the MATLAB Application Program Interface Guide for more information.)
For Fortran MEX-files: If running a Fortran MEX file causes a segmentation violation, general protection fault, or bus error, make sure you have declared all real variables as real*8. On some platforms, the default is real*4. This is too small for 64-bit floating-point representation in MATLAB.
For all MEX-files: Use print statements throughout your code and/or the debugger to isolate the problematic code. On UNIX, gdb and dbx are popular debuggers for this. On the PC, you can use Microsoft Visual C or any other IDE debugger. Using s statements allows you to track variable contents in order to determine where in your code the MEX-file fails or what part of the code is being executed. For more details, please refer to the MATLAB Application Program Interface Guide.
For more information on debugging MEX-files in general, please see Tech Note
1608 on our Web site, at
www.mathworks.com/support/tech-notes/1600/1605.html.
What can I do if I get a segmentation violation when running M-files?
Occasionally segmentation violations occur while you are running M-files. MATLAB performs all its memory allocations and de-allocations internally, and a segmentation violation that occurs when you are running M-files can usually be traced back to a built-in function.
If the error is caused by one of our functions, we may already know about it, and we may have a solution available. To see if this is the case, try looking up the error in our Solution Search Engine on our Web site (www.mathworks.com/support/). Tip: Try searching under "segmentation violation" and the name of the function.
If you can't find the information you need in our Solution Search, try reproducing the error on your machine and on other machines running the same version of MATLAB. If you can reproduce the error on your machine but not on others, try reinstalling MATLAB, as some of the files on your machine may be corrupt. If you can reproduce the problem in all cases, please let us know (www.mathworks.com/support/) so we can try to address it. Please include all the information re-quested in the error message, so that we can easily reproduce the problem and find a quicker fix to the problem.
What can I do if I get a segmentation violation when printing?
In most cases, segmentation violations that occur during printing are a result of conflicts with printer drivers. Certain printer drivers make unexpected modifications to the FPU (floating point unit) of the machine, which results in a segmen-tation violation. The best way around this is to try a different printer driver. Contact your printer vendor for advice on obtain-ing an appropriate driver.
Faulty video card drivers have been known to cause this problem, not only when you are printing, but also when you are doing extensive graphics manipulations. The best solution is to update your video card driver. Contact your video card vendor for more information on how to do this.
Why do you ask us to contact you in the segmentation violation error message?
The main reason is because we want to know when these segmentation violations occur. If there is a bug with one of our functions, we want to be able to address it as soon as possible. In earlier releases, the error message was less detailed and as a result, was less helpful to the customer. Now we ask for several pieces of information in the error message to help us reproduce the problem in-house, because the sooner we can reproduce the problem, the sooner we can attempt to fix it.
The important point is, if you experience segmentation violations and can reproduce them consistently, then please let us know so that we can address the issue. You do not need to contact us if we have already provided a solution to the problem in our Solution Search engine or if the problem cannot be reproduced - as would be the case if the error were caused by a memory leak in your MEX-file or by a faulty installation. The key is that it must be reproducible for us to be able to fix it. This is why we ask for all the information in the error message.