Using the C Math Library    

Avoiding Memory Leaks in Your Functions

Follow these recommendations to avoid the memory leaks.

  1. Never call a library function without assigning the array it returns to an array variable (by calling mlfAssign()) or without embedding the call as an argument to a library function.

    Memory leak:

    The array returned by mlfSin() is not bound to a variable and never freed.

  1. Never assign a value to an array variable without subsequently deleting the array.

    Memory leak:

    You must pair each mxArray* declaration with a call to mxDestroyArray().

  1. Never use the assignment operator to assign array values.

    Unexpected termination of your program:

    x is a temporary array. If x is subsequently passed as an input argument to a function, that function will delete x.Any subsequent reference to x will cause your program to crash.


 Automated Memory Management Using Automated Memory Management