Mex File crashes after some runs

5 visualizzazioni (ultimi 30 giorni)
Matteo Zecchin
Matteo Zecchin il 4 Dic 2017
Risposto: Rick il 6 Giu 2018
Hello everybody, I've recently started using mex functions but I've encounter some problems that I cannot solve. In particular when calling in Matlab the following mex function inside a loop it often crashes with the access violation error. I've checked the indices of the variables and to free the memory asap but it keeps giving the same error. Hoping someone of you can help me. Thanks!
#include "mex.h"
#include "matrix.h"
#include <math.h>
/* The gateway function */
int mod (int a, int b)
{
if(b < 0)
return mod(a, -b);
int ret = a % b;
if(ret < 0)
ret+=b;
return ret;
}
double sumLog(double mi, double ma, double *l)
{
int index=round(abs(mi-ma)*100);
if(index>1999)
index=1999;
if(mi>ma)
{ double temp=mi;
mi=ma;
ma=temp;
}
return ma+l[index];
}
double glcalc(double sigma, double rl, double y)
{
return -pow((rl-y),2)/(2*pow(sigma,2));
}
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
if(nrhs != 8)
mexErrMsgIdAndTxt("MyToolbox:BCJR:nrhs","6 inputs required: U,RL1,RL2,StUp,FOut,Sigma");
int ulength= mxGetN(prhs[0]);
double *u=mxGetPr(prhs[0]);
int rl1length= mxGetN(prhs[1]);
double *rl1=mxGetPr(prhs[1]);
int rl2length= mxGetN(prhs[2]);
double *rl2=mxGetPr(prhs[2]);
double *StUp=mxGetPr(prhs[3]);
const mwSize *StUpDim= mxGetDimensions(prhs[3]);
double *Theta=mxGetPr(prhs[4]);
const mwSize *ThetaDim= mxGetDimensions(prhs[4]);
double sigma=mxGetScalar(prhs[5]);
double f1=mxGetScalar(prhs[6]);
double f2=mxGetScalar(prhs[7]);
if(ulength != rl1length || ulength!= rl2length)
mexErrMsgIdAndTxt("MyToolbox:BCJR:nrhs","u,rl1,rl2 must be of the same length");
if(StUpDim[0] != ThetaDim[0])
mexErrMsgIdAndTxt("MyToolbox:BCJR:nrhs","StUp and Theta must have same dimension");
/*Qu Messages*/
mxArray *qup=mxCreateDoubleMatrix(ulength,2, mxREAL);
if(!qup)
mexErrMsgIdAndTxt("MyToolbox:BCJR:nrhs","qup NULL");
double *qu=mxGetPr(qup);
mxArray *qu1p=mxCreateDoubleMatrix(ulength,2, mxREAL);
if(!qu1p)
mexErrMsgIdAndTxt("MyToolbox:BCJR:nrhs","qu1p NULL");
double *qu1=mxGetPr(qu1p);
mxArray *qu2p=mxCreateDoubleMatrix(ulength,2, mxREAL);
if(!qu2p)
mexErrMsgIdAndTxt("MyToolbox:BCJR:nrhs","qu2p NULL");
double *qu2=mxGetPr(qu2p);
int i,j;
for(i=0; i<ulength;i++)
{
qu[2*i]=glcalc(sigma,u[i],-1);
qu[2*i+1]=glcalc(sigma,u[i],1);
if(qu[2*i]>qu[2*i+1])
{
qu[2*i+1]=qu[2*i+1]-qu[2*i];
qu[2*i]=0;
}
else
{
qu[2*i]=qu[2*i]-qu[2*i+1];
qu[2*i+1]=0;
}
}
for(i=0; i<ulength;i++)
{
qu1[2*i]=qu[2*i];
qu1[2*i+1]=qu[2*i+1];
}
mxArray *gl1p=mxCreateDoubleMatrix(rl1length,2*StUpDim[0], mxREAL);
if(!gl1p)
mexErrMsgIdAndTxt("MyToolbox:BCJR:nrhs","gl1p NULL");
double *gl1= mxGetPr(gl1p);
double max;
int states=(StUpDim[0]);
for(i=0; i<ulength;i++)
{
max=-DBL_MAX;
for(j=0; j<states; j++)
{
gl1[i*(states*2)+2*j]=glcalc(sigma,rl1[i],(Theta[j]-0.5)*2);
gl1[i*(states*2)+2*j+1]=glcalc(sigma,rl1[i],(Theta[j+(ThetaDim[0])]-0.5)*2);
if(gl1[i*(states*2)+2*j]>max)
max=gl1[i*(states*2)+2*j];
if(gl1[i*(states*2)+2*j+1]>max)
max=gl1[i*(states*2)+2*j+1];
}
for(j=0; j<states; j++)
{
gl1[i*(states*2)+2*j]=gl1[i*(states*2)+2*j]-max;
gl1[i*(states*2)+2*j+1]=gl1[i*(states*2)+2*j+1]-max;
}
}
mxArray *gl2p=mxCreateDoubleMatrix(rl2length,2*StUpDim[0], mxREAL);
if(!gl2p)
mexErrMsgIdAndTxt("MyToolbox:BCJR:nrhs","gl2p NULL");
double *gl2= mxGetPr(gl2p);
for(i=0; i<ulength;i++)
{
max=-DBL_MAX;
for(j=0; j<states; j++)
{
gl2[i*(states*2)+2*j]=glcalc(sigma,rl2[i],(Theta[j]-0.5)*2);
gl2[i*(states*2)+2*j+1]=glcalc(sigma,rl2[i],(Theta[j+(ThetaDim[0])]-0.5)*2);
if(gl2[i*(states*2)+2*j]>max)
max=gl2[i*(states*2)+2*j];
if(gl2[i*(states*2)+2*j+1]>max)
max=gl2[i*(states*2)+2*j+1];
}
for(j=0; j<states; j++)
{
gl2[i*(states*2)+2*j]=gl2[i*(states*2)+2*j]-max;
gl2[i*(states*2)+2*j+1]=gl2[i*(states*2)+2*j+1]-max;
}
}
mxArray *Fp=mxCreateDoubleMatrix(ulength,states, mxREAL);
if(!Fp)
mexErrMsgIdAndTxt("MyToolbox:BCJR:nrhs","Fp NULL");
double *F=mxGetPr(Fp);
mxArray *Bp=mxCreateDoubleMatrix(ulength,states, mxREAL);
if(!Bp)
mexErrMsgIdAndTxt("MyToolbox:BCJR:nrhs","Bp NULL");
double *B=mxGetPr(Bp);
mxArray *Ep=mxCreateDoubleMatrix(ulength,2, mxREAL);
if(!Ep)
mexErrMsgIdAndTxt("MyToolbox:BCJR:nrhs","Ep NULL");
double *E=mxGetPr(Ep);
mxArray *F2p=mxCreateDoubleMatrix(ulength,states, mxREAL);
if(!F2p)
mexErrMsgIdAndTxt("MyToolbox:BCJR:nrhs","F2p NULL");
double *F2=mxGetPr(F2p);
mxArray *B2p=mxCreateDoubleMatrix(ulength,states, mxREAL);
if(!B2p)
mexErrMsgIdAndTxt("MyToolbox:BCJR:nrhs","B2p NULL");
double *B2=mxGetPr(B2p);
mxArray *E2p=mxCreateDoubleMatrix(ulength,2, mxREAL);
if(!E2p)
mexErrMsgIdAndTxt("MyToolbox:BCJR:nrhs","E2p NULL");
double *E2=mxGetPr(E2p);
mxArray *LogP=mxCreateDoubleMatrix(2000,1, mxREAL);
if(!LogP)
mexErrMsgIdAndTxt("MyToolbox:BCJR:nrhs","Logp NULL");
double *Logarithm=mxGetPr(LogP);
for(i=0;i<2000;i++)
{
Logarithm[i]=log(1+exp(-i*0.01));
}
int iterations;
for(iterations=0; iterations<20; iterations++)
{
for(i=0;i<ulength*states;i++)
{
F[i]=-300;
}
F[0]=0;
for(i=1;i<ulength;i++)
{
max=-DBL_MAX;
for(j=0;j<states;j++)
{
F[i*states+(int)StUp[j]]=sumLog(F[i*states+(int)StUp[j]],qu1[2*(i-1)]+F[(i-1)*states+j]+gl1[(i-1)*states*2+2*j],Logarithm);
F[i*states+(int)StUp[j+StUpDim[0]]]=sumLog(F[i*states+(int)StUp[j+StUpDim[0]]],qu1[2*(i-1)+1]+F[(i-1)*states+j]+gl1[(i-1)*states*2+2*j+1],Logarithm);
}
for(j=0;j<states;j++)
{
if(F[i*states+j]>max)
max=F[i*states+j];
}
for(j=0;j<states;j++)
{
F[i*states+j]=F[i*states+j]-max;
}
}
for(i=0;i<ulength*states;i++)
{
B[i]=-300;
}
B[(ulength-1)*states]=0;
for(i=1;i<ulength;i++)
{
max=-DBL_MAX;
for(j=0;j<states;j++)
{
B[(ulength-i-1)*states+j]=sumLog(B[(ulength-i-1)*states+j],qu1[(ulength-i)*2]+B[(ulength-i)*states+(int)StUp[j]]+gl1[(ulength-i)*states*2+2*j],Logarithm);
B[(ulength-i-1)*states+j]=sumLog(B[(ulength-i-1)*states+j],qu1[(ulength-i)*2+1]+B[(ulength-i)*states+(int)StUp[j+StUpDim[0]]]+gl1[(ulength-i)*states*2+2*j+1],Logarithm);
}
for(j=0;j<states;j++)
{
if(B[(ulength-i-1)*states+j]>max)
max=B[(ulength-i-1)*states+j];
}
for(j=0;j<states;j++)
{
B[(ulength-i-1)*states+j]=B[(ulength-i-1)*states+j]-max;
}
}
for(i=0;i<ulength*2;i++)
{
E[i]=-300;
}
for(i=0;i<ulength;i++)
{
for(j=0;j<states;j++)
{
E[i*2]=sumLog(E[i*2],F[i*states+j]+B[i*states+(int)StUp[j]]+gl1[i*states*2+j*2],Logarithm);
E[i*2+1]=sumLog(E[i*2+1],F[i*states+j]+B[i*states+(int)StUp[j+StUpDim[0]]]+gl1[i*states*2+j*2+1],Logarithm);
}
if(E[i*2]>E[i*2+1])
{
E[i*2+1]=E[i*2+1]-E[i*2];
E[i*2]=0;
}
else
{
E[i*2]=E[i*2]-E[i*2+1];
E[i*2+1]=0;
}
}
for(i=0;i<ulength;i++)
{
qu2[2*i]=sumLog(qu[mod((f1*i+pow(i,2)*f2),ulength-3)*2],E[mod((f1*i+pow(i,2)*f2),ulength-3)*2],Logarithm);
qu2[2*i+1]=sumLog(qu[mod((f1*i+pow(i,2)*f2),ulength-3)*2+1],E[mod((f1*i+pow(i,2)*f2),ulength-3)*2+1],Logarithm);
if(qu2[2*i]>qu2[2*i+1])
{
qu2[2*i+1]=qu2[2*i+1]-qu2[2*i];
qu2[2*i]=0;
}
else
{
qu2[2*i]=qu2[2*i]-qu2[2*i+1];
qu2[2*i+1]=0;
}
}
for(i=0;i<ulength*states;i++)
{
F2[i]=-300;
}
F2[0]=0;
for(i=1;i<ulength;i++)
{
max=-DBL_MAX;
for(j=0;j<states;j++)
{
F2[i*states+(int)StUp[j]]=sumLog(F2[i*states+(int)StUp[j]],qu2[2*(i-1)]+F2[(i-1)*states+j]+gl2[(i-1)*states*2+2*j],Logarithm);
F2[i*states+(int)StUp[j+StUpDim[0]]]=sumLog(F2[i*states+(int)StUp[j+StUpDim[0]]],qu2[2*(i-1)+1]+F2[(i-1)*states+j]+gl2[(i-1)*states*2+2*j+1],Logarithm);
}
for(j=0;j<states;j++)
{
if(F2[i*states+j]>max)
max=F2[i*states+j];
}
for(j=0;j<states;j++)
{
F2[i*states+j]=F2[i*states+j]-max;
}
}
for(i=0;i<ulength*states;i++)
{
B2[i]=-300;
}
B2[(ulength-1)*states]=0;
for(i=1;i<ulength;i++)
{
max=-DBL_MAX;
for(j=0;j<states;j++)
{
B2[(ulength-i-1)*states+j]=sumLog(B2[(ulength-i-1)*states+j],qu2[(ulength-i)*2]+B2[(ulength-i)*states+(int)StUp[j]]+gl2[(ulength-i)*states*2+2*j],Logarithm);
B2[(ulength-i-1)*states+j]=sumLog(B2[(ulength-i-1)*states+j],qu2[(ulength-i)*2+1]+B2[(ulength-i)*states+(int)StUp[j+StUpDim[0]]]+gl2[(ulength-i)*states*2+2*j+1],Logarithm);
}
for(j=0;j<states;j++)
{
if(B2[(ulength-i-1)*states+j]>max)
max=B2[(ulength-i-1)*states+j];
}
for(j=0;j<states;j++)
{
B2[(ulength-i-1)*states+j]=B2[(ulength-i-1)*states+j]-max;
}
}
for(i=0;i<ulength*2;i++)
{
E2[i]=-300;
}
for(i=0;i<ulength;i++)
{
for(j=0;j<states;j++)
{
E2[i*2]=sumLog(E2[i*2],F2[i*states+j]+B2[i*states+(int)StUp[j]]+gl2[i*states*2+j*2],Logarithm);
E2[i*2+1]=sumLog(E2[i*2+1],F2[i*states+j]+B2[i*states+(int)StUp[j+StUpDim[0]]]+gl2[i*states*2+j*2+1],Logarithm);
}
if(E2[i*2]>E2[i*2+1])
{
E2[i*2+1]=E2[i*2+1]-E2[i*2];
E2[i*2]=0;
}
else
{
E2[i*2]=E2[i*2]-E2[i*2+1];
E2[i*2+1]=0;
}
}
for(i=0;i<ulength;i++)
{
qu1[mod((f1*i+pow(i,2)*f2),ulength-3)*2*2]=sumLog(qu[mod((f1*i+pow(i,2)*f2),ulength-3)*2],E2[2*i],Logarithm);
qu1[mod((f1*i+pow(i,2)*f2),ulength-3)*2+1]=sumLog( qu[mod((f1*i+pow(i,2)*f2),ulength-3)*2+1],E2[i*2+1],Logarithm);
}
}
plhs[0] = mxCreateDoubleMatrix(ulength-3,1,mxREAL);
double *Errors = mxGetPr(plhs[0]);
Errors[0]=0;
mxArray *InputHatp=mxCreateDoubleMatrix(ulength-3,2, mxREAL);
if(!InputHatp)
mexErrMsgIdAndTxt("MyToolbox:BCJR:nrhs","InputHatp NULL");
double *InputHat= mxGetPr(InputHatp);
for(i=0;i<ulength-3;i++)
{
InputHat[mod((f1*i+pow(i,2)*f2),ulength-3)*2]= sumLog(E[mod((f1*i+pow(i,2)*f2),ulength-3)*2],qu[mod((f1*i+pow(i,2)*f2),ulength-3)*2]+E2[2*i],Logarithm);
InputHat[mod((f1*i+pow(i,2)*f2),ulength-3)*2+1]= sumLog(E[mod((f1*i+pow(i,2)*f2),ulength-3)*2+1],qu[mod((f1*i+pow(i,2)*f2),ulength-3)*2+1]+E2[2*i+1],Logarithm);
}
for(i=0;i<ulength-3;i++)
{
if(InputHat[i*2]>InputHat[i*2+1])
{
Errors[i]=0;
}
else
{
Errors[i]=1;
}
}
mxDestroyArray(qup);
mxDestroyArray(qu1p);
mxDestroyArray(qu2p);
mxDestroyArray(gl1p);
mxDestroyArray(gl2p);
mxDestroyArray(Fp);
mxDestroyArray(Bp);
mxDestroyArray(Ep);
mxDestroyArray(F2p);
mxDestroyArray(B2p);
mxDestroyArray(E2p);
mxDestroyArray(InputHatp);
mxDestroyArray(LogP);
return;
}
and this is the error log
MATLAB crash file:C:\Users\kille\AppData\Local\Temp\matlab_crash_dump.8464-1:
------------------------------------------------------------------------ Access violation detected at Mon Dec 04 18:55:02 2017 ------------------------------------------------------------------------
Configuration: Crash Decoding : Disabled Crash Mode : continue (default) Current Graphics Driver: NVIDIA Corporation GeForce GTX 960M/PCIe/SSE2 Version 4.5.0 NVIDIA 384.77 Default Encoding : windows-1252 Graphics card 1 : Intel Corporation ( 0x8086 ) Intel® HD Graphics 530 Version 21.20.16.4664 Graphics card 2 : NVIDIA ( 0x10de ) NVIDIA GeForce GTX 960M Version 22.21.13.8477 Host Name : DESKTOP-KLMUST0 MATLAB Architecture : win64 MATLAB Root : C:\Program Files (x86)\MatLab MATLAB Version : 8.6.0.267246 (R2015b) OpenGL : hardware Operating System : Microsoft Windows 10 Home Processor ID : x86 Family 6 Model 94 Stepping 3, GenuineIntel Virtual Machine : Java 1.7.0_60-b19 with Oracle Corporation Java HotSpot™ 64-Bit Server VM mixed mode Window System : Version 10.0 (Build 16299)
Fault Count: 1
Abnormal termination: Access violation
Register State (from fault): RAX = ffffffffee7c4dd8 RBX = 00000000ff990bc0 RCX = 000000000000001c RDX = 00000000000007cf RSP = 00000000043fb030 RBP = 00000000043fb0b0 RSI = 0000000000000002 RDI = 00000000e58b3800
R8 = 00000000ff990bc0 R9 = 0000000000000003
R10 = 0000000026240280 R11 = 00000000043fb040
R12 = 00000000e58b3888 R13 = 000000001ebec310
R14 = 00000000e58b3850 R15 = 00000000ff924dc0
RIP = 000000001e8f14bc EFL = 00010296
CS = 0033 FS = 0053 GS = 002b
Stack Trace (from fault): [ 0] 0x000000001e8f14bc C:\Users\kille\OneDrive\Documenti\Università\Magistrale\ChannelCoding\LTE_Project SpeedUp2\TURBO2.mexw64+00005308 [ 1] 0x000000001e8f1f97 C:\Users\kille\OneDrive\Documenti\Università\Magistrale\ChannelCoding\LTE_Project SpeedUp2\TURBO2.mexw64+00008087 mexFunction+00002713 [ 2] 0x00000000fc5f7551 C:\Program Files (x86)\MatLab\bin\win64\libmex.dll+00095569 mexRunMexFile+00000129 [ 3] 0x00000000fc5f6872 C:\Program Files (x86)\MatLab\bin\win64\libmex.dll+00092274 inSwapMexfileReader+00000594 [ 4] 0x00000000fc5f6426 C:\Program Files (x86)\MatLab\bin\win64\libmex.dll+00091174 mexUnlock+00004998 [ 5] 0x0000000016e44cc3 C:\Program Files (x86)\MatLab\bin\win64\m_dispatcher.dll+00019651 Mfh_file::dispatch_fh_impl+00000531 [ 6] 0x0000000016e4488e C:\Program Files (x86)\MatLab\bin\win64\m_dispatcher.dll+00018574 Mfh_file::dispatch_fh+00000062 [ 7] 0x0000000016e451e8 C:\Program Files (x86)\MatLab\bin\win64\m_dispatcher.dll+00020968 Mfunction_handle::dispatch+00000968 [ 8] 0x0000000017857942 C:\Program Files (x86)\MatLab\bin\win64\m_lxe.dll+00162114 [ 9] 0x000000001788ab5f C:\Program Files (x86)\MatLab\bin\win64\m_lxe.dll+00371551 [ 10] 0x0000000017889810 C:\Program Files (x86)\MatLab\bin\win64\m_lxe.dll+00366608 [ 11] 0x00000000178b96e1 C:\Program Files (x86)\MatLab\bin\win64\m_lxe.dll+00562913 [ 12] 0x00000000178b9406 C:\Program Files (x86)\MatLab\bin\win64\m_lxe.dll+00562182 [ 13] 0x000000001798f71b C:\Program Files (x86)\MatLab\bin\win64\m_lxe.dll+01439515 MathWorks::lxe::ReleaseCurrentMcrContext+00545643 [ 14] 0x000000001785a1e8 C:\Program Files (x86)\MatLab\bin\win64\m_lxe.dll+00172520 [ 15] 0x000000001785bd7d C:\Program Files (x86)\MatLab\bin\win64\m_lxe.dll+00179581 [ 16] 0x0000000016e44dd6 C:\Program Files (x86)\MatLab\bin\win64\m_dispatcher.dll+00019926 Mfh_file::dispatch_fh_impl+00000806 [ 17] 0x0000000016e4488e C:\Program Files (x86)\MatLab\bin\win64\m_dispatcher.dll+00018574 Mfh_file::dispatch_fh+00000062 [ 18] 0x0000000016e451e8 C:\Program Files (x86)\MatLab\bin\win64\m_dispatcher.dll+00020968 Mfunction_handle::dispatch+00000968 [ 19] 0x00000000178566de C:\Program Files (x86)\MatLab\bin\win64\m_lxe.dll+00157406 [ 20] 0x000000001785839d C:\Program Files (x86)\MatLab\bin\win64\m_lxe.dll+00164765 [ 21] 0x000000001788ab5f C:\Program Files (x86)\MatLab\bin\win64\m_lxe.dll+00371551 [ 22] 0x0000000017889810 C:\Program Files (x86)\MatLab\bin\win64\m_lxe.dll+00366608 [ 23] 0x00000000178b96e1 C:\Program Files (x86)\MatLab\bin\win64\m_lxe.dll+00562913 [ 24] 0x00000000178b9406 C:\Program Files (x86)\MatLab\bin\win64\m_lxe.dll+00562182 [ 25] 0x000000001791c23f C:\Program Files (x86)\MatLab\bin\win64\m_lxe.dll+00967231 MathWorks::lxe::ReleaseCurrentMcrContext+00073359 [ 26] 0x000000001791c146 C:\Program Files (x86)\MatLab\bin\win64\m_lxe.dll+00966982 MathWorks::lxe::ReleaseCurrentMcrContext+00073110 [ 27] 0x000000001791bc56 C:\Program Files (x86)\MatLab\bin\win64\m_lxe.dll+00965718 MathWorks::lxe::ReleaseCurrentMcrContext+00071846 [ 28] 0x00000000178a0b14 C:\Program Files (x86)\MatLab\bin\win64\m_lxe.dll+00461588 [ 29] 0x00000000178a0adc C:\Program Files (x86)\MatLab\bin\win64\m_lxe.dll+00461532 [ 30] 0x00000000178a091a C:\Program Files (x86)\MatLab\bin\win64\m_lxe.dll+00461082 [ 31] 0x00000000178a0802 C:\Program Files (x86)\MatLab\bin\win64\m_lxe.dll+00460802 [ 32] 0x0000000016fc60a1 C:\Program Files (x86)\MatLab\bin\win64\m_interpreter.dll+00680097 inEvalCmdWithLocalReturn+00000065 [ 33] 0x00000000fb6029bd C:\Program Files (x86)\MatLab\bin\win64\libmwbridge.dll+00076221 mnGetPrompt+00003597 [ 34] 0x00000000fb603597 C:\Program Files (x86)\MatLab\bin\win64\libmwbridge.dll+00079255 mnParser+00001079 [ 35] 0x0000000016da87a4 C:\Program Files (x86)\MatLab\bin\win64\mcr.dll+00362404 mcrInstance::mnParser_on_interpreter_thread+00000036 [ 36] 0x0000000016d7c477 C:\Program Files (x86)\MatLab\bin\win64\mcr.dll+00181367 mcr::runtime::setInterpreterThreadToCurrent+00019751 [ 37] 0x0000000016d7c4b3 C:\Program Files (x86)\MatLab\bin\win64\mcr.dll+00181427 mcr::runtime::setInterpreterThreadToCurrent+00019811 [ 38] 0x0000000016d7ccb1 C:\Program Files (x86)\MatLab\bin\win64\mcr.dll+00183473 mcr::runtime::setInterpreterThreadToCurrent+00021857 [ 39] 0x00000000fab62aa6 C:\Program Files (x86)\MatLab\bin\win64\uiw.dll+00535206 UIW_AttachThreadInput+00001270 [ 40] 0x00000000fab62335 C:\Program Files (x86)\MatLab\bin\win64\uiw.dll+00533301 wsd_to_MSW+00004181 [ 41] 0x00000000fab623b9 C:\Program Files (x86)\MatLab\bin\win64\uiw.dll+00533433 wsd_to_MSW+00004313 [ 42] 0x00007ffafcb1c835 C:\WINDOWS\System32\USER32.dll+00510005 keybd_event+00000453 [ 43] 0x00007ffafcac0b19 C:\WINDOWS\System32\USER32.dll+00133913 GetPropW+00000649 [ 44] 0x00007ffafcac0aa5 C:\WINDOWS\System32\USER32.dll+00133797 GetPropW+00000533 [ 45] 0x00007ffaff103b14 C:\WINDOWS\SYSTEM32\ntdll.dll+00670484 KiUserCallbackDispatcher+00000036 [ 46] 0x00007ffafc1f10a4 C:\WINDOWS\System32\win32u.dll+00004260 NtUserPeekMessage+00000020 [ 47] 0x00007ffafcac02ed C:\WINDOWS\System32\USER32.dll+00131821 PeekMessageW+00000381 [ 48] 0x00007ffafcac0156 C:\WINDOWS\System32\USER32.dll+00131414 PeekMessageA+00000278 [ 49] 0x00000000fab0e1cb C:\Program Files (x86)\MatLab\bin\win64\uiw.dll+00188875 UIW_ShowMenuItem+00005547 [ 50] 0x00000000fab634c2 C:\Program Files (x86)\MatLab\bin\win64\uiw.dll+00537794 UIW_SuspendAttachThreadInput+00000690 [ 51] 0x0000000012750ad3 C:\Program Files (x86)\MatLab\bin\win64\libmwservices.dll+01510099 services::system_events::PpeDispatchHook::dispatchOne+00032291 [ 52] 0x000000001275c785 C:\Program Files (x86)\MatLab\bin\win64\libmwservices.dll+01558405 sysq::addProcessPendingEventsUnitTestHook+00006101 [ 53] 0x000000001275c830 C:\Program Files (x86)\MatLab\bin\win64\libmwservices.dll+01558576 sysq::addProcessPendingEventsUnitTestHook+00006272 [ 54] 0x000000001275e475 C:\Program Files (x86)\MatLab\bin\win64\libmwservices.dll+01565813 sysq::getCondition+00004197 [ 55] 0x000000001275fe22 C:\Program Files (x86)\MatLab\bin\win64\libmwservices.dll+01572386 svWS_ProcessPendingEvents+00000162 [ 56] 0x0000000016d7d3cd C:\Program Files (x86)\MatLab\bin\win64\mcr.dll+00185293 mcr::runtime::setInterpreterThreadToCurrent+00023677 [ 57] 0x0000000016d7e03a C:\Program Files (x86)\MatLab\bin\win64\mcr.dll+00188474 mcr::runtime::setInterpreterThreadToCurrent+00026858 [ 58] 0x0000000016d75c95 C:\Program Files (x86)\MatLab\bin\win64\mcr.dll+00154773 mcr_process_events+00007557 [ 59] 0x0000000016d775a2 C:\Program Files (x86)\MatLab\bin\win64\mcr.dll+00161186 mcr_process_events+00013970 [ 60] 0x00000000150c37ae C:\Program Files (x86)\MatLab\bin\win64\MVMLocal.dll+00210862 mvm_server::inproc::LocalFactory::terminate+00081678 [ 61] 0x00000000fa924d99 C:\Program Files (x86)\MatLab\bin\win64\mvm.dll+01002905 mvm::detail::initLocalMvmHack+00000537 [ 62] 0x00000000fa925368 C:\Program Files (x86)\MatLab\bin\win64\mvm.dll+01004392 mvm::detail::SessionImpl::privateSession+00000376 [ 63] 0x00000000fa9255b2 C:\Program Files (x86)\MatLab\bin\win64\mvm.dll+01004978 mvm::detail::SessionImpl::privateSession+00000962 [ 64] 0x0000000140006247 C:\Program Files (x86)\MatLab\bin\win64\MATLAB.exe+00025159 [ 65] 0x0000000140007cf4 C:\Program Files (x86)\MatLab\bin\win64\MATLAB.exe+00031988 [ 66] 0x00007ffafd0b1fe4 C:\WINDOWS\System32\KERNEL32.DLL+00073700 BaseThreadInitThunk+00000020 [ 67] 0x00007ffaff0cef91 C:\WINDOWS\SYSTEM32\ntdll.dll+00454545 RtlUserThreadStart+00000033
This error was detected while a MEX-file was running. If the MEX-file is not an official MathWorks function, please examine its source code for errors. Please consult the External Interfaces Guide for information on debugging MEX-files.
If this problem is reproducible, please submit a Service Request via: http://www.mathworks.com/support/contact_us/
A technical support engineer might contact you with further information.
Thank you for your help.
  3 Commenti
Matteo Zecchin
Matteo Zecchin il 5 Dic 2017
By inspection, in the sense that for each loop I've computed the extremal values of the counter variable and compared it with the size of the arrays that I was reading/writing. My level of attention was not suffic though, in fact in the proximity of the end of the program there is a typo, a *2 is repeated two times, and eventually it was the problem. I would consider the question solved but I want to ask you something since you are surely more expert than me. I've declared qu2p as a DoubleMatrix without setting its cells to defined values, could this be a problem when I'm reading it? or by default it is initialized to a prefixed value?
Jan
Jan il 6 Dic 2017
@Matteo: This is a lot of code for asking a forum. Could you narrow down the problem by your own a little bit? The simplest way would to insert mexPrintf to find, where the code crashes.
Some ideas:
  • Do not rely on size_t and mwSize to be int. Prefer:
size_t ulength = mxGetN(prhs[0]);
const mwSize *StUpDim= mxGetDimensions(prhs[3]);
mwSize states=(StUpDim[0]); % not: int
  • Do not collect a huge number of complicated code lines in one big function. Excessive index manipulations like
qu1[mod((f1*i+pow(i,2)*f2),ulength-3)*2*2]=sumLog(qu[mod((f1*i+pow(i,2)*f2),ulength-3)*2],E2[2*i],Logarithm);
are such prone to typos, that is is impossible to check the correctness except with an exhaustive unit-testing. But for a unit-test, the program must be split into parts, which can be tested separately from each other.
  • Reduce the code size e.g. by moving the allocation into a subfunction:
mxArray *InputHatp=mxCreateDoubleMatrix(ulength-3,2, mxREAL);
if(!InputHatp)
mexErrMsgIdAndTxt("MyToolbox:BCJR:nrhs","InputHatp NULL");
But remember, that mxCreateDoubleMatrix stops with an error message already, when it is not successful in Mex functions.
If it would be my job to debug this code, the first thing I'd do is to delete it and rewrite it from scratch. It is very likely that it contains at least one index error. But the complexity of the code is such high, that debugging is too cruel.

Accedi per commentare.

Risposte (1)

Rick
Rick il 6 Giu 2018
I sometimes have the same problem with my Fortran mex function........ I am not going to go through your code to make sure your not trying to access memory outside your array bounds. But I will offer a suggestion..........After each run on the command line issue a "clear mex" command. That should clear out any extras that may be in a buffer memory.... If you havn't already, give that a shot.
Later, Rick Wojo

Categorie

Scopri di più su Write C Functions Callable from MATLAB (MEX Files) in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by