run time error when using a mex file, maybe due to memory allocation

混江龙づ霸主 提交于 2020-01-25 21:24:12

问题


I've written a mex function named mx_minimum_power that I'm calling it in MATLAB as follows:

[Fs,Fd,pow_remained] = mx_minimum_power(A11,A12_real,A12_imag,A13_real,A13_imag,A22,A23_real,A23_imag,A33,MatSize);  
  • A11, A12_real, A13_real, A22, A23_real, A33 are 30555x1 single matrices
  • A12_imag, A13_imag, A23_imag are 1x1 single variables
  • MatSize is a 1x1 double variable with the value 30555, that is the size of matrices

In an iteration, each time the hermitian matrix A is constructed


and eigenvalues and eigenvectors of it should be found. So I've written the following gateway function with the help of CVM Class Library.

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    double *arraysizePtr = NULL;
    arraysizePtr = mxGetPr(prhs[9]);
    const int arraysize = (int)*arraysizePtr;
    const int matrixDimention = 3;
    float *inMatrixA11 = (float *)mxGetPr(prhs[0]);
    float *inMatrixA12_real = (float *)mxGetPr(prhs[1]);
    float *inMatrixA12_imag = (float *)mxGetPr(prhs[2]);
    float *inMatrixA13_real = (float *)mxGetPr(prhs[3]);
    float *inMatrixA13_imag = (float *)mxGetPr(prhs[4]);
    float *inMatrixA22 = (float *)mxGetPr(prhs[5]);
    float *inMatrixA23_real = (float *)mxGetPr(prhs[6]);
    float *inMatrixA23_imag = (float *)mxGetPr(prhs[7]);
    float *inMatrixA33 = (float *)mxGetPr(prhs[8]);
    basic_schmatrix< float, complex<float> > A(matrixDimention);
    basic_scmatrix< float, complex<float> > EigenVectors(matrixDimention);
    basic_rvector<float>EigenValues(matrixDimention);
    int i = 0;
    for (i = 0; i < arraysize; i++)
    {
        A.set(2, 2, inMatrixA11[i]);
        A.set(2, 3, complex<float>(inMatrixA12_real[i], inMatrixA12_imag[0]));
        A.set(2, 4, complex<float>(inMatrixA13_real[i], inMatrixA13_imag[0]));
        A.set(3, 3, inMatrixA22[i]);
        A.set(3, 4, complex<float>(inMatrixA23_real[i], inMatrixA23_imag[0]));
        A.set(4, 4, inMatrixA33[i]);
        try{ EigenValues = A.eig(EigenVectors);}
        catch (cvmexception& ex) {
            cout << ex.what() << endl;
        }
    }
}

until line 28 that is try{ EigenValues = A.eig(EigenVectors);}, here I get the run-time error:

and the control jumps to line 311 of the file cvm.h, that is line 11 of the following code:

CVM_NAMESPACE_BEG  

//! %Array deleter helper class  
template<typename T>
class ArrayDeleter {
public:  
    void operator () (T* d) const {
        if (d != nullptr) {
            ::delete[] d;
        }
    }
};

seems that something causes the array get deleted before returning the control to the gateway function but I don't know what? Because if I stop debugging and return to MATLAB, I will get:

and if I press attempt to continue, I'll get:

so there's no way to see which exception is thrown through the try, catch and cout code.

Hint:

  • basic_schmatrix is a class in CVM library that is encapsulating hermitian matrices of complex numbers. See here, here and here
  • basic_scmatrix is a class in CVM library that encapsulates square matrices of complex numbers. See here, here and here, I've used it to allocate memory for Eigenvectors, because they'll be returned in the form of a the square matrix

    in which the columns are orthonormal eigenvectors.
  • basic_rvector is a class in CVM library for encapsulating vector of real numbers. It has been used to allocate memory for eigenvalues because as we know the eigenvalues of a hermitian matrix is real and they'll be returned in the form of the vector

  • I've used indices 2-4 instead of 1-3 because as I've explained here CVM0=1

  • Finaly eig is a member function of the class basic_schmatrix that gets the address to the allocated memory for eigenvectors and returns an object of type rvector, please see here

EDIT :
from my inspections it's not an error. The control should normally reach line 311 of the file CVM.h, because it is using dynamic memory allocation and it's going to release resources when returning to the gateway function here's a list of where the control goes when you press F11s and it's not throwing an exception at all. But what's wrong with my gateway function code that causes matlab encounter such a breakpoint?

来源:https://stackoverflow.com/questions/41423499/run-time-error-when-using-a-mex-file-maybe-due-to-memory-allocation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!