问题描述
我的用C ++ / CLI编写,并调用以C#编写的DLL。 / p>
当gc新对象时,mexFunction返回时是否应该被垃圾收集?
它的引用应该丢失,但似乎没有垃圾收集...每次调用mex函数增加的内存分配(没有,内存不用于MATLAB变量)。
我已经尝试创建一个大的虚拟值窄范围,当通过MEX文件时,我可以看到分配和释放的内存。但是不是那样在mexFunction =(
)中创建的主对象我试图在析构函数和终结器中删除它,但我不能得到它的垃圾
我不认为外部DLL文件是问题,为了说明,我创建了这个傻的mexFunction:
public ref class Foo
{
public:
Foo()
{
Dictionary< int,String ^> ^ bar = gcnew Dictionary< int,String ^> ;;
for(int i = 0; i< 10000000; i ++)
{
bar-> Add(i,abcdefghijklmnopqrstuvxyz);
}
}
};
void mexFunction(int nlhs,mxArray * plhs [] ,int nrhs,mxArray * prhs [])
{
Foo ^ test = gcnew Foo();
}
这会使MATLAB的内存大约300 MB,虽然后续调用不会像我真正的MEX文件中那样增加内存。
编辑:
我回答了自己的问题,罪魁祸首是 mxArrayToString
我发现了问题,结果是它不是.net相关的...对不起,红鲱鱼
因为我不使用新的,malloc或mxMalloc我错误地假设所有我的非托管内存将在堆栈和清理,当mexFunction结束。
但是mxArrayToString不返回指向MATLAB数据的指针,因为mxGetData和其他mx *函数。它将数据复制到堆上,并且必须调用mxFree释放它。我使用mxArrayToString作为输入创建一个System :: String ^,唯一需要的更改是保存一个时间字符指针,使用它为String ^构造函数,然后mxFree它。
所以再一次为SEO: mxArrayToString 的指针需要 mxFree'd !
My MEX file is written in C++/CLI and calls a DLL written in C#.
When gcnew'ing an object, shouldn't it be garbage collected when the mexFunction returns?Its references should be lost but nothing seems to be garbage collected... each call to the mex function increases MATLAB's memory allocation (and no, the memory is not used for MATLAB variables).
I've experimented with creating a large dummy value with narrow scope and when stepping through the MEX file I can see the memory allocated and released. But not so with the main object created in the mexFunction =(
I've tried to delete it in the destructor and finalizer, but I can't get it to garbage collect. How can I free the managed memory when returning to MATLAB?
I don't think external DLL filers are the problem. To illustrate, I created this silly mexFunction:
public ref class Foo
{
public:
Foo()
{
Dictionary<int,String^>^ bar = gcnew Dictionary<int,String^>;
for(int i=0;i<10000000;i++)
{
bar->Add(i, "abcdefghijklmnopqrstuvxyz");
}
}
};
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, mxArray* prhs[])
{
Foo^ test = gcnew Foo();
}
This bumps MATLAB's memory by around 300 MB, although subsequent calls don't increase the memory further like in my real MEX file.
EDIT:
I answered my own question, the culprit was mxArrayToString
I found the problem, turns out it wasn't .net related after all... sorry for that red herring
Since I wasn't using new, malloc or mxMalloc I wrongly assumed that all my unmanaged memory would be in the stack and cleaned up when the mexFunction ended.
However mxArrayToString doesn't return a pointer to the MATLAB data as mxGetData and other mx* functions do. It copies the data onto the heap and one has to call mxFree to release it. I used mxArrayToString as input to create a System::String^, the only change needed was to save a temporay char pointer, use that for the String^ constructor and then mxFree it.
So once again for SEO: The pointer from mxArrayToString needs to be mxFree'd!
这篇关于MATLAB中的内存泄漏> MEX文件>受管DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!