Search code examples
apimatlabc-api

MATLAB C API: What does mxDestroyArray(NULL) do?


I use MATLAB's mxDestroyArray() function and wonder how to do proper error handling.

If I called mxCreate*Array() or similiar, I get a valid pointer on success and NULL on failure, i. e. if memory is full.

If I create several arrays in this way and at least one fails, I would like to free all what I don't need any longer.

Here I am wondering: Do I need to explicitly check every value?

if (error) {
    if (a) mxDestroyArray(a);
    if (b) mxDestroyArray(b);
    if (c) mxDestroyArray(c);
}

or can I just omit the checks?

if (error) {
    mxDestroyArray(a);
    mxDestroyArray(b);
    mxDestroyArray(c);
}

Solution

  • Simply try it and see if matlab crashes. I think you need the check as you also do in plain C as null isn't referencing a valid memory address