2つの配列とスカラーを入力とし、数学的計算を行った後にスカラー量を出力として返すmex関数(C言語)を作成しました。対応するmex関数をMATLABプラットフォーム上でコンパイルすることはできますが、入力データでMATLABを実行するとすぐに、MATLABにクラッシュする原因となります。エラーログには、「4月25日に検出されたセグメンテーション違反...:..:.. 2016」という見出しがあります。また、GNUデバッガ「gdb」を使ってLinuxプラットフォームでデバッグしようとしました。これは、nrhs、prhs []、nlhs、plhs []を使用して入出力引数の数と型を検証するために使用したすべてのif文に問題があることを示しています。たとえば、入力引数の数をチェックするための私の最初のステートメントは、一部のデータでmex関数を実行しているときにMatlabがクラッシュする
if(nrhs!=3)
mexErrMsgTxt("Error..Three inputs required.");
と同様にnlhsのものです。 GNUデバッガは上記のif文に最初のブレークポイントを設定していますが、もしコメントしていれば、これに続く2番目のif文にも問題があります。私がすべてのif文をコメントアウトしているとき、mex関数は正常に実行されており、また、私に望ましい出力を与えてくれます。
私は利用可能なすべての回答を読むことでこのバグを削除しようとして以来、長いことがありましたが、私はそうすることができません。上記の問題で私を助けてください。 ありがとうございます。無効なアドレスでmxWhateverFunction(plhs[0]) <-- fill in for whatever
結果はplhs[0]
をテストする前に、任意の変数にリンクされていないときので、上記のコメントで述べたように
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
double *Ip, *Is; /* Input data vectors */
double r; /* Value of r (input) */
double *dist; /* Output ImED distance */
size_t ncols; /* For storing the size of input vector */
/* Checking for proper number of arguments */
if(nrhs!=3)
mexErrMsgTxt("Error..Three inputs required.");
if(nlhs!=1)
mexErrMsgTxt("Error..Only one output required.");
/* make sure the first input argument(value of r) is scalar */
if(!mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]) || mxGetNumberOfElements(prhs[0])!=1)
mexErrMsgTxt("Error..Value of r must be a scalar.");
/* make sure that the input vectors are of type double */
if(!mxIsDouble(prhs[1]) || mxIsComplex(prhs[1]) || !mxIsDouble(prhs[2]) || mxIsComplex(prhs[2]))
mexErrMsgTxt("Error..Input vectors must be of type double.");
/* Make sure that the output is of type double and is a scalar */
if(!mxIsDouble(plhs[0]) || mxIsComplex(plhs[0]) || mxGetNumberOfElements(plhs[0])!=1)
mexErrMsgTxt("Error..Image Euclidean Distance must be a scalar.");
/* check that number of rows in input arguments is 1 */
if(mxGetM(prhs[1])!=1 || mxGetM(prhs[2])!=1)
mexErrMsgTxt("Error..Inputs must be row vectors.");
/* Get the value of r */
r = mxGetScalar(prhs[0]);
/* Getting the input vectors */
Ip = mxGetPr(prhs[1]);
Is = mxGetPr(prhs[2]);
ncols = mxGetN(prhs[1]);
/* Creating link for the scalar output */
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
dist = mxGetPr(plhs[0]);
imedDistCal(r,Ip,Is,(mwSize)ncols,dist);
}
エラーログから、保護された、読み取り専用の、または割り当てられていないメモリにアクセスしようとしているようです。私はループとインデックスをチェックすることから始めます。 – ritualmagick
実際のコードも表示してください。 – rayryeng
@rayryeng実際のコードをご覧ください。 – nagarwal