You have memory leaks in your current code. E.g.,
tmpResult = mxMalloc(numRows*numRows*sizeof(*tmpResult)); // this memory gets leaked by the very next line
tmpResult = r8mat_expm1(numRows, inputMat); // leaks memory from the mxMalloc( ) call
plhs[0] = mxCreateDoubleMatrix(numRows, numRows, mxREAL); // this memory gets leaked by the very next line
mxSetDoubles(plhs[0], tmpResult); // leaks memory from the mxCreateDoubleMatrix( ) call
This leaked memory is bad but does not cause the crash.
It appears you attach native C/C++ allocated memory to an mxArray, and this will cause a crash. You can't mix native C/C++ allocated memory in an mxArray. All memory in an mxArray must come from MATLAB API memory allocation functions.
You really need to post some details about the r8mat_expm1( ) function for us to be sure about what the real problem is. But from your description it sounds like it allocates the memory for the result using native C/C++ routines (e.g., malloc or calloc) and then returns the pointer to that, which you then attach to an mxArray ... resulting in a crash. If this is true, then you will either need to change the function to use MATLAB API allocation functions, or copy the results. To copy the results you would do something like this:
#include <string.h> // for memcpy( )
#include <stdlib.h> // for free( )
double *tmpResult;
double *inputMat;
double *pr;
int numRows;
numRows = mxGetM(prhs[0]);
inputMat = mxGetDoubles(prhs[0]);
tmpResult = r8mat_expm1(numRows, inputMat); // calculate matrix exponential
plhs[0] = mxCreateDoubleMatrix(numRows, numRows, mxREAL);
pr = mxGetDoubles(plhs[0]);
memcpy(pr,tmpResult,numRows*numRows*sizeof(double));
free(tmpResult); // use free( ) for native C/C++ allocated memory
To avoid the data copy and use MATLAB API allocation functions, you would need to alter the source code of r8mat_expm1( ) and pass in the pointer instead of allocating memory inside the function. Then your mexFunction( ) code would look like this instead of the above:
double *inputMat;
double *pr;
int numRows;
numRows = mxGetM(prhs[0]);
inputMat = mxGetDoubles(prhs[0]);
plhs[0] = mxCreateDoubleMatrix(numRows, numRows, mxREAL);
pr = mxGetDoubles(plhs[0]);
r8mat_expm1(numRows, inputMat, pr); // calculate matrix exponential
And the signature for the r8mat_expm1( ) function would change to this:
void r8mat_expm1(int, double *, double *);
where the 3rd input argument is used inside r8mat_expm1( ) for the output, and no memory allocation is done inside this function.
Another way to do this is to change the malloc( ) and calloc( ) inside r8mat_expm1( ) to be mxMalloc( ) and mxCalloc( ) instead, and then attach that result to the mxArray. If you want I can post that code, but frankly it is more complicated than what I have posted above so I would see no practical reason to code it this way.
Best Answer