User`s guide

7 Libraries
About Memory Management and Cleanup
In this section...
“Overview” on page 7-38
“Passing m x Arrays to Share d Libraries” on page 7-3 8
Overview
Generated C++ code provides consistent garbage collection via the object
destructors and the MCR’s internal memory manager optimizes to avoid
heap fragmentation.
If mem ory constraints a re still present on your system, try preallocating
arrays in M. This will reduce the number of calls to the memory manager, and
thedegreetowhichtheheapfragments.
Passing mxArrays to Shared Libraries
When an mxArray is created in an application w hich uses the MCR, it is
created in the managed mem ory space of the MCR.
Therefore, it is very important that you never create
mxArrays(orcallany
other MathWorks function) before calling
mclInitializeApplication.
It is safe to call
mxDestroyArray when you n o longe r need a particular
mxArray in your code, even when the input has been assigned to a persistent
or global variable in MATLAB. MATLAB utilizes reference counting to ensure
that when
mxDestroyArray is called, if another reference to the underlying
data still exists, the memory will not be freed. Even if the underlying memory
is not freed, the
mxArray passe d to mxDestr oyArray wil l no longer be valid.
For more information abo ut
mclInitializeApplication and
mclTerminateApplication, see “Calling a Shared Library” on page 7-11.
7-38