How do I check memory allocation to debug my MEX-file?

After I create a C-MEX file, every time I run the compiled code, MATLAB becomes very unstable. For example, I tried running the command "profile on" after running the MEX-file and received an 'Assertion detected' error. Alternatively, my MEX-file may produce a segmentation violation at run-time. I would like to check memory allocation in order to try debugging these issues.

 Akzeptierte Antwort

MEX-files can corrupt memory and cause MATLAB to crash later on. One way to debug this type of problem is to run MATLAB in check_malloc mode. When run in this mode, MATLAB attaches a header and footer to each piece of allocated memory. This can then be used to determine if someone is writing past the end of the array that was allocated.
Note that MATLAB will run slower when in check_malloc mode.
To run in check_malloc mode on the PC, execute the following at the DOS prompt (Start->Run...->type 'cmd'->Ok):
set MATLAB_MEM_MGR=debug
matlab
On Linux systems, you can use the mtrace(3) utility to facilitate malloc debugging. See the Linux MAN page for mtrace(3) for more information about this tool.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB Compiler finden Sie in Hilfe-Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by