Matlab crashed

5 views (last 30 days)
Jane Jean
Jane Jean on 26 Mar 2012
Hello, I am experiencing crashes from Matlab while running mex programs. Below is the scenario of the crashes.
A.mexw32 runs properly in the beginning. So I started working on B.cpp which will later be used to generate B.mexw32. However, while working on B.cpp, there were several memory allocation problems and Matlab crashed a few times. Then when I tried to execute A.mexw32 which had not at all been modified, the execution crashed too. I was guessing that this was due to some cache memory leakage so I restarted the computer. But the crashes still happen while executing A.mexw32. After a several attempts, the crashes still continue.
I then used another computer to execute the same A.mexw32. It works perfectly.
My only guess is that some memory blocs are corrupted while I was working on B.cpp. But why is A.mexw32 still not working after restarting the computer?

Answers (3)

James Tursa
James Tursa on 26 Mar 2012
You very likely have coding errors in A.mexw32 and B.mexw32 and it is corrupting memory and causing crashes. The reason it is intermittent is because the corruption itself does not immediately cause a crash ... it is only when downstream code (either in the mex routine itself or in MATLAB after you returned from the mex routine) accesses that corrupted memory that you get the seg fault. Memory leaks will not cause a crash ... they will simply use up memory and leave it inaccessible to your process. If the code is short enough you can post it and we can look for coding errors. If it is not short enough, try to reduce it to a small program that reproduces the crash and post that.

Jane Jean
Jane Jean on 27 Mar 2012
Thank you for the answer.
A.mexw32 runs perfectly on other machines. However, after a day not using this computer, after restarting this morning, running A.mexw32 still caused matlab crash at the first attempt. Then after closing Matlab and restarting it, A.mexw32 runs perfectly.
A.c is too long to be posted here. B.c is slighly shorter but still long. I am not able to shorten it as I can't detect where exactly is the error in the code. All lines were executed correctly before the crash. Even the final answer was correct. But at the end of the execution of B.mexw32, Matlab just crashed.
void kronProduct1(int rowA, int colA, int rowB, int colB, double *A, double *B, double *C)
{
double *B1;
int i, j, k, l, m;
B1 = malloc(rowB*colB*sizeof(double));
for (i=0; i<rowA; i++){
for (j=0; j<colA; j++){
for (m=0; m<rowB*colB; m++){
B1[m] = B[m];
}
matrixScalarMultiplication(rowB, colB, B1, A[i*colA+j]);
for (k=0; k<rowB; k++){
for (l=0; l<colB; l++){
C[i*rowB*colB+k*colB+l]=B1[k*colB+l];
}
}
}
}
free(B1);
return ;
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mxArray *outputRandn[1];
mxArray *inputRandn[1], *inputN_vis, *inputLambda, *inputN_ep;
double *arrayN_vis, *arrayLambda, *arrayN_ep;
double *arrayInput_Randn, *N, *A;
double *B, *C, *D;
double *outputArray;
int i, j, rowLen, colLen;
inputN_vis = prhs[0];
inputLambda = prhs[1];
inputN_ep = prhs[2];
arrayN_vis = mxGetPr(inputN_vis);
arrayLambda = mxGetPr(inputLambda);
arrayN_ep = mxGetPr(inputN_ep);
// Preparing input and output of the Matlab function "randn".
inputRandn[0] = mxCreateDoubleMatrix(1, 2, mxREAL);
arrayInput_Randn = (double*)mxGetPr(inputRandn[0]);
arrayInput_Randn[0] = arrayN_vis[0]-1;
arrayInput_Randn[1] = 1;
if (mexCallMATLAB(1, outputRandn, 1, inputRandn, "randn") != 0)
{
mexErrMsgTxt("Error when calling randn");
}
N= mxGetPr(outputRandn[0]);
matrixScalarMultiplication(arrayN_vis[0], 1, N, 100);
roundMatrix(arrayInput_Randn[0]*arrayInput_Randn[1], N);
D = malloc(arrayInput_Randn[0]*arrayInput_Randn[0]*sizeof(double));
B = malloc(2*arrayInput_Randn[0]*arrayInput_Randn[0]*sizeof(double));
C = malloc(arrayN_ep[0]*1);
generateZeros(2*arrayInput_Randn[0], arrayInput_Randn[0], B);
generateIdentity(arrayInput_Randn[0], D);
generateOnes(arrayN_ep[0], 1, C);
matrixScalarMultiplication(arrayInput_Randn[0], arrayInput_Randn[0], D, arrayLambda[0]);
for(i=0; i<arrayInput_Randn[0]*arrayInput_Randn[0]; i++){
B[i] = D[i];
}
A = malloc(arrayN_ep[0]*2*arrayInput_Randn[0]*1*arrayInput_Randn[0]*sizeof(double));
kronProduct1(arrayN_ep[0], 1, 2*arrayInput_Randn[0], arrayInput_Randn[0], C, B, A);
rowLen = arrayN_ep[0]*2*arrayInput_Randn[0];
colLen = 1*arrayInput_Randn[0];
plhs[0] = mxCreateDoubleMatrix(rowLen, colLen, mxREAL);
outputArray = mxGetPr(plhs[0]);
for(i=0;i<rowLen;i++)
{
for(j=0;j<colLen;j++)
{
outputArray[(j*rowLen)+i] = A[(i*colLen)+j];
}
}
free(B);
free(D);
free(C);
free(A);
return;
}
  5 Comments
Jane Jean
Jane Jean on 28 Mar 2012
Thank you for the answer again. I'll make sure that I format the code the next time. :)
I tried using a debugger but Matlab still crashed and the debugger has a memory error. mexPrintf at every single line until right before "return;" but everything was fine. The correct result was returned and Matlab crashed right after.
If i use mxMalloc and mxFree, do I still need to check if they are successful like in the case of malloc and free?
Jane Jean
Jane Jean on 28 Mar 2012
I tried using Microsoft Visual Studio for the debugging, but I got this error. How should I interpret this? All I know is that some memory errors have occured. But how to know exactly where?
Unhandled exception at 0x322ea263 in MATLAB.exe: 0xC0000005: Access violation reading location 0xffffffffffffffff.

Sign in to comment.


Jane Jean
Jane Jean on 28 Mar 2012
MATLAB code
for i=1:10
i
clear all;
load('parameters.mat')
blaslib = fullfile('C:\Program Files\MATLAB\R2011b\extern\lib\win64\microsoft\libmwblas.lib');
lapacklib = fullfile('C:\Program Files\MATLAB\R2011b\extern\lib\win64\microsoft\libmwlapack.lib');
mex('-largeArrayDims', '-compatibleArrayDims', 'geometryMatrix64.c', 'mathFunction64.c', blaslib, lapacklib)
[Geoc] = geometryMatrix64(lat, long, incl, peri, seax, GM, r_E, ele_mask, time, theta);
end;
On the command screen... i =
1
Calculating geometry matrix...
geometry matrix done!
i =
2
  1 Comment
Jan
Jan on 29 Mar 2012
The error appears randomly. This is often caused by using uninitialized variables. This can happen in any subfunction.

Sign in to comment.

Categories

Find more on Function Creation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!