I have been trying to figure this out for quite some time.
I use a MEX file in matlab (Linux 64bit) which uses CUDA. The code compiles and executes fine but when I want to unload the mex (e.g. to recompile it or when matlab exits), matlab crashes immediately without any message and with an empty dump.
I was able reduce it to a minimal working example:
MEX cpp File:
#include <stdint.h>
#include "mex.h"
extern "C" void cudaTest();
void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
cudaTest();
}
CUDA File compiled with NVCC:
void cudaTest() {
float* d_test = NULL;
cudaMalloc((void**) &d_test, 10000 * sizeof(float));
cudaFree(d_test);
}
While with my real program it always crashes, with this minimal example it is not always reproducible. Sometimes it does crash sometimes not..
I think this solved my problem:
http://www.mathworks.de/matlabcentral/answers/45307