Mex文件仅在第二次使用时给出分段错误

时间:2011-04-20 00:34:52

标签: matlab segmentation-fault mex

以下代码成功编译并在第一次调用时返回正确的结果。第二次进行相同的调用,我得到了一个分段错误错误。

//% function TF = InWindow(Date,WindowStartDates,WindowEndDates,EndHint)
//% INWINDOW returns true for window that contains Date. All inputs must be 
//% uint32 and WindowEndDates must be sorted.

//% EndHint is an optional input that specifies the row number to start
//% searching from.

#include "mex.h"
#include "matrix.h"
#include "math.h"

void CalculationRoutine(mxLogical *ismember, uint32_T *Date, uint32_T *WindowStartDates, uint32_T *WindowEndDates, unsigned int *StartIndex, int NumObs) {

mwIndex Counter;

// Find the first window that ends on or after the date. 
for (Counter = (mwIndex) *StartIndex; Counter < NumObs; Counter++) {  
    if (*Date <= *(WindowEndDates+Counter)) {
        break;
    }
}
*StartIndex = (unsigned int) Counter;

// Now flag every observation within the window. Remember that WindowStartDates
// is not necessarily sorted (but WindowEndDates is).
for (Counter = (mwIndex) *StartIndex; Counter < NumObs; Counter++) { 
    if (*Date >= *(WindowStartDates+Counter)) {
        *(ismember+Counter) = true;
    }
}

}

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

    mxArray *ismember;
    unsigned int *StartIndex;

    //Input Checking.

    if (nrhs == 3) {
        // Default Hint to first entry.        
        mexPrintf("SI Starts OK.\n");
        *StartIndex = 1;
        mexPrintf("SI Ends OK.\n");
    } else if (nrhs == 4) {
        if (!mxIsUint32(prhs[3])) {
            mexErrMsgTxt("EndHint must be uint32.");
        } 
        StartIndex = mxGetData(prhs[3]);
    } else {
        mexErrMsgTxt("Must provide three or four input arguments.");
    }
    // Convert the hint to base-zero indexing.
    *StartIndex = *StartIndex - 1;

    // Check the inputs for the window range.
    if (!mxIsUint32(prhs[0])) {
        mexErrMsgTxt("DatesList must be uint32.");
    }
    if (!mxIsUint32(prhs[1])) {
        mexErrMsgTxt("WindowStartsDates must be uint32.");
    }
    if (!mxIsUint32(prhs[2])) {
        mexErrMsgTxt("WindowEndsDates must be uint32.");
    }

    if (mxGetM(prhs[1]) != mxGetM(prhs[2])) {
        mexErrMsgTxt("WindowStartDates must be the same length as WindowEndDates.");
    }    

    // Prepare the output array.
    ismember = mxCreateLogicalArray(mxGetNumberOfDimensions(prhs[1]), mxGetDimensions(prhs[1]));

    CalculationRoutine(mxGetLogicals(ismember),mxGetData(prhs[0]), 
        mxGetData(prhs[1]), mxGetData(prhs[2]), StartIndex, (int) mxGetM(prhs[1]));

    plhs[0] = ismember;
}

我称之为:

>>InWindow(uint32(5),uint32((1:6)'),uint32((3:8)'))

代码在分段错误之前到达两个mexPrintf调用之间的行(即第一个调用打印,但不是第二个)。

我在Matlab 2007a(是的,我知道),Win7 64bit和VS 2008。

1 个答案:

答案 0 :(得分:4)

你需要初始化指针StartIndex - 你很幸运它第一次工作,因为它没有指向一个已定义的内存位置。为什么不做更多的事情:

unsigned int StartIndex;
// and either:
StartIndex = 1;
// or:
StartIndex = * (static_cast< unsigned int * >( mxGetData(prhs[3]) ) );