Question: I have written following code in MATLAB:

```
#include <string.h>
#include <stdlib.h>
#include "mex.h"
#include "matrix.h"

void mexFunction(int nlhs, mxArray *plhs[],
                 int nrhs, const mxArray *prhs[]){

    double* x = (double*)malloc(5 * sizeof(*x));
    int k;
    for(k=0;k<5;k++)
        x[k] = k;
    mxArray* p1 = mxCreateDoubleMatrix(5, 1, mxREAL);
    mxSetPr(p1, x);
}

```

I call the function with no input or output. The code really does nothing except initializing some vector. I understand that the code has serious memory leak, but aside from memory leaks, when I call the function, MATLAB closes.

If I replace `malloc` with `mxMalloc`, the code will be executed with no problem.
I understand that using mx\* for allocating memory is preferable, but I am sure that malloc and calloc along with free can be used in MEX with no problem.

I also removed (double\*), but still it crashes. 

It is appreciated if let me know why the mentioned code results in MATLAB crashing.
Thanks

Answer: 
There are actually two issues with the code posted:

```
double* x = (double*)malloc(5 * sizeof(*x));
   :
mxArray* p1 = mxCreateDoubleMatrix(5, 1, mxREAL);
mxSetPr(p1, x); /* This leaks memory AND causes a crash */

```

The first issue is what others have already posted. You can never attach native C/C++ memory (malloc, calloc, realloc, new, etc) to an mxArray. To do so will screw up the MATLAB Memory Manager and eventually lead to a crash when MATLAB tries to free this memory. You can only attach memory from a MATLAB API function to an mxArray.

The second issue is that there is a memory leak. The mxCreateDoubleMatrix(5, 1, mxREAL) call allocates memory behind the data pointer in the mxArray. This data pointer is NOT on the garbage collection list. When you subsequently call mxSetPr(p1, x) you wipe out that data pointer and replace it with x, so your only access to that data pointer is lost and you leak the memory. Since this data pointer is not on the garbage collection list, it will not be freed when the mex function returns to the caller. It can only be recovered by restarting MATLAB. Here are some ways to properly code this without causing a crash and without causing a memory leak:

Change to mxMalloc and mxFree the original data pointer:

```
double* x = (double*)mxMalloc(5 * sizeof(*x));
   :
mxArray* p1 = mxCreateDoubleMatrix(5, 1, mxREAL);
mxFree(mxGetData(p1)); /* Free the original data pointer */
mxSetPr(p1, x); /* Attach MATLAB API generated pointer */

```

Change to mxMalloc and start with empty matrix:

```
double* x = (double*)mxMalloc(5 * sizeof(*x));
   :
mxArray* p1 = mxCreateDoubleMatrix(0, 0, mxREAL);
mxSetPr(p1, x); /* Attach MATLAB API generated pointer */
mxSetM(p1, 5); /* Set row size */
mxSetN(p1, 1); /* Set column size */

```

Don't separately allocate at all ... just use the original data pointer:

```
mxArray* p1 = mxCreateDoubleMatrix(5, 1, mxREAL);
double* x = (double*)mxGetData(p1);
/* Then use x downstream in your code */

```
To save someone else potential hours of pain, a function using mxMalloc(x) on an input parameter x will only work if x is declared as const int x, as opposed to merely int x.