使用C ++中的Matlab函数进行内存泄漏

时间:2015-11-15 19:07:36

标签: c++ matlab memory-leaks mex

我在Matlab中编写了自己的函数,并将其编译为动态C ++库。然后在循环中从我的实际C ++代码中调用它。 我可以看到我的程序正在快速占用所有内存。 在所有计算之后,我尝试在Matlab函数中调用clear方法,但它对内存效率没有任何影响。 简化代码如下(没有包含所有内容,因为问题不在于它们)。

void my_function(args)
{

    mwArray sum; //it is a vector of 3 doubles
    mwArray fina(final_function); //final_function is of std::string type
    minimize(1, sum, fina); //minimize is my own Matlab function about which I told before, it returns a vector of 3 variables and puts it into variable "sum"
    double tetta = sum(1);
    double fi = sum(2);
    double d = sum(3);
    ...

}

int main(int argc, char** argv)
{
    if( !mclInitializeApplication(NULL,0) )
    {
        std::cout << "Could not initialize the application.\n";
        exit(1);
    }
    if (!libminimInitialize())
    {
        std::cout << "Could not initialize the library.\n";
        exit(1);
    }
    for (x = 0; x < 100000; x++)
    {
        my_function(args);
    }
    libminimTerminate();
    mclTerminateApplication();

}

我的matlab函数看起来像那样:

function xy = minimize(text)
fun = str2func(text);
options = optimset('Display','off','Algorithm','active-set', 'TolFun', 0.001);
lb = [0, -1.6*pi/180, 0];
ub = [22*pi/180, 1.6*pi/180, 50];
x0 = [0, 0, 0];
A = [];
b = [];
Aeq = [];
beq = [];
non1con = [];
xy = fmincon(fun, x0,A,b,Aeq,beq,lb,ub,non1con, options);
clear -regexp ^(?!xy).*;
end

0 个答案:

没有答案