The ability to use Purify to detect memory leaks in a MEX function running in MATLAB is not supported, although you may be able to do this under limited circumstances.
You are free to start MATLAB from Purify and then run the MEX file to detect memory leaks, but MathWorks does not provide technical support and there is a significant chance that it will not work.
If you do not need Java to run your code, MATLAB is more likely to work with Purify. In order to this, start MATLAB with the -nojvm flag. It may also help to use the -noaccel flag to turn off the JIT. In practice, MATLAB is such a large and complex program that Purify cannot always run it.
We have found that when starting MATLAB from Purify using -nojvm and -noaccel, it seems to start fine from Purify, and you can do some basic operations, but more complex operations such as loading Simulink result in a segmentation violation. As a result, it is not possible to use purify with S-functions.
In a very simple scenario we were able to load MATLAB and execute a simple MEX function using the following procedure:
1. In Purify select File->Run
Under Program Name: $MATLABROOT\bin\win32\MATLAB.exe
Under Command Line arguments: -nojvm –noaccel
2. Execute the MEX-file and analyze output in Valgrind.
($MATLABROOT is the MATLAB root directory on your machine, as returned by typing:
at the MATLAB command prompt.)
Alternately you may also use BoundsChecker or Insure to profile the MEX files. BoundsChecker works well with MATLAB and in most cases is able to run MATLAB with JAVA turned on. However, we have found that it does not detect memory leaks efficiently. Overall Insure seems to work the best when compared to Purify and BoundsChecker. Insure runs well on both release mode and Debug mode and does a good job of detecting both memory leaks and access violations.