mxSetCell( CellArray_ptr, i, A );
mxDestroyArray( A ); // get rid of this line
Get rid of the mxDestroyArray call. When you call the mxSetCell function, three things happen:
1) The address of A gets put into the i'th cell location in the CellArray_ptr variable
2) The type of A gets changed from "temporary" to "sub-element"
3) The address of A gets removed from the garbage collection list.
After the mxSetCell function call, A is literally now part of the CellArray_ptr array. Its ultimate clearing will depend entirely on what happens to CellArray_ptr. When you call mxDestroyArray on A, you are invalidating part of a legitimate variable. Hence when MATLAB tries to access or clear CellArray_ptr later on it crashes since it accesses invalid memory.