mex 文件 运行 内存不足

mex file run out of memory

我用c++代码写了一个mexfunction,然后编译得到mexa64文件。在我多次使用 mexa64 文件后,我的 PC 内存 运行 用完了。

这是我的 mexfunction:

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
//  double *res ;// the final result index 
    int status1,status2,status3;
    double *res ;
    double *out_mat;
    double *x_idx,*y_idx,*width_idx,*height_idx;
    char* sequence_path;// the tracking image sequences
    char* sequence_name;// the tracking image sequences


    char* output_path;// save the output    
    int start_frame=(int)mxGetScalar(prhs[0]);
    int end_frame=(int)mxGetScalar(prhs[1]);
    int Max_track_number=(int)mxGetScalar(prhs[2]);     

     /* Extract the inputs */   
    int buflen1=(mxGetM(prhs[3])*mxGetN(prhs[3]))+1;
    sequence_path= (char *)mxCalloc(buflen1,sizeof(char));   
    status1=mxGetString(prhs[3],sequence_path,buflen1);

    int buflen2=(mxGetM(prhs[4])*mxGetN(prhs[4]))+1;
    sequence_name= (char *)mxCalloc(buflen2,sizeof(char));   
    status2=mxGetString(prhs[4],sequence_name,buflen2);


    int buflen3=(mxGetM(prhs[5])*mxGetN(prhs[5]))+1;
     output_path= (char *)mxCalloc(buflen3,sizeof(char));      
     status3=mxGetString(prhs[5],output_path,buflen3);
         if(status3!=0)
    mexWarnMsgTxt("Not enough space. String is truncated.");

    float x=(float)mxGetScalar(prhs[6]);
    float y=(float)mxGetScalar(prhs[7]);
    float width=(float)mxGetScalar(prhs[8]); 
    float height=(float)mxGetScalar(prhs[9]); 




     x_idx=mxGetPr(prhs[6]);
     y_idx=mxGetPr(prhs[7]);
     width_idx=mxGetPr(prhs[8]);
     height_idx=mxGetPr(prhs[9]);

     mexPrintf("the dimension is %f,%f,%f,%f\n",*x_idx,*y_idx,*width_idx,*height_idx);



    int N1=mxGetM(prhs[6])*mxGetN(prhs[6]);
    int N2=mxGetM(prhs[7])*mxGetN(prhs[7]);
    int N3=mxGetM(prhs[8])*mxGetN(prhs[8]);
    int N4=mxGetM(prhs[9])*mxGetN(prhs[9]);
    if(N1!=N2|N2!=N3|N3!=N4)
    mexErrMsgTxt("the dimension of the initial bounding box parameter is wrong");
    int NN=N1;

     plhs[0] = mxCreateDoubleMatrix(Max_track_number*5*NN,1,mxREAL);
     out_mat = mxGetPr(plhs[0]);

     plhs[1] = mxCreateDoubleMatrix(1,1,mxREAL);
     res = mxGetPr(plhs[1]);


 *res=struck_track(start_frame,end_frame,Max_track_number,sequence_path,sequence_name,output_path,out_mat,x_idx,y_idx,width_idx,height_idx,NN);

}

是否有一些问题会导致内存耗尽?

您应该在使用以下内容时使用 mxFree:

mxCalloc mxMalloc mxRealloc mxArrayToString

我从你的代码中可以看出你正在使用 mxCalloc,但没有相应的 mxFree。

除了使用 mxFree 之外,您还可以增加 Java 堆大小。您可以在设置、常规、java 堆内存下找到它。