MATLAB Application Program Interface | Search  Help Desk |
mxFree | See Also |
Free dynamic memory allocated by mxCalloc
subroutine mxFree(ptr) integer*4 ptrptr
mxCalloc
.
mxFree
deallocates heap space. mxFree
frees memory using MATLAB's own memory management facility. This ensures correct memory management in error and abort (Ctrl-C) conditions.
mxFree
works differently in MEX-files than in stand-alone MATLAB applications. With MEX-files, mxFree
returns to the heap any memory allocated using mxCalloc
. If you do not free memory with this command, MATLAB frees it automatically on return from the MEX-file. In stand-alone MATLAB applications, you have to explicitly free memory, and MATLAB memory management is not used.
In a MEX-file, your use of mxFree
depends on whether the specified memory parcel is persistent or nonpersistent. By default, memory parcels created by mxCalloc
are nonpersistent.
The MATLAB 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 a good programming practice to deallocate memory just as soon as you are through using it. Doing so generally makes the entire system run more efficiently.
When a MEX-file completes, the MATLAB 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 clean-up handler. Then, the clean-up handler calls mxFree
.
mxCalloc
, mxFreeMatrix