| Contents | Index |
#include "matrix.h" void mxFree(void *ptr);
subroutine mxFree(ptr) mwPointer ptr
mxFree deallocates heap space using the MATLAB memory management facility. This ensures correct memory management in error and abort (Ctrl+C) conditions.
To deallocate heap space, MATLAB applications in C should always call mxFree rather than the ANSI C free function.
In MEX-files, but excluding MAT or engine standalone applications, the MATLAB memory management facility maintains a list of all memory allocated by mxCalloc, mxMalloc, and mxRealloc. The memory management facility automatically deallocates all of a MEX-file's managed parcels when the MEX-file completes and control returns to the MATLAB prompt. mxFree also removes the memory parcel from the memory management facility's list of memory parcels.
When mxFree appears in a MAT or engine standalone MATLAB application, it simply deallocates the contiguous heap space that begins at address ptr.
In MEX-files, your use of mxFree depends on whether the specified memory parcel is persistent or nonpersistent. By default, memory parcels created by mxCalloc, mxMalloc, and mxRealloc are nonpersistent. The memory management facility automatically frees all nonpersistent memory whenever a MEX-file completes. Thus, even if you do not call mxFree, MATLAB takes care of freeing the memory for you. Nevertheless, it is good programming practice to deallocate memory as soon as you are through using it. Doing so generally makes the entire system run more efficiently.
If an application calls mexMakeMemoryPersistent, the specified memory parcel becomes persistent. When a MEX-file completes, the memory management facility does not free persistent memory parcels. Therefore, the only way to free a persistent memory parcel is to call mxFree. Typically, MEX-files call mexAtExit to register a cleanup handler. The cleanup handler calls mxFree.
See the following examples in matlabroot/extern/examples/mx.
See the following examples in matlabroot/extern/examples/refbook.
See the following examples in matlabroot/extern/examples/mex.
mexAtExit, mexMakeArrayPersistent, mexMakeMemoryPersistent, mxCalloc, mxDestroyArray, mxMalloc, mxRealloc

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |