Using the C Math Library    

Using Automated Memory Management

You must follow a set procedure when you write a function that conforms to the rules of automated memory management. The pattern is the same for each function.

  1. Declare the interface for your function:
  2. Initialize local array variables to NULL or to valid arrays:

    Library functions, including mlfAssign(), require that output arguments are initialized to NULL or to a valid array.

  1. Call mlfEnterNewContext() to turn on automated memory management for your function and to change the status of temporary input arrays to bound arrays:

    Pair this call with a call to mlfRestorePreviousContext() at the end of your function.

  1. Perform the work of your function, using mlfAssign(), rather than the assignment operator (=), to assign values to arrays.
  2. Free any bound array variables by calling mxDestroyArray():

    However, do not destroy the return value from your function.

  1. Call mlfRestorePreviousContext() to reset the state for each input array the value it had on entering the function and then to delete any temporary arrays:

    Pass the same arguments that you passed to mlfEnterNewContext().

  1. Call mlfReturnValue() to make the array you are returning temporary and then return the temporary array:

 Avoiding Memory Leaks in Your Functions Function Template