Mex动态内存管理问题与std :: vector在链接外部DLL;分段错误 [英] Mex dynamic memory management issue with std::vector in linked external DLL; Segmentation error

查看:192
本文介绍了Mex动态内存管理问题与std :: vector在链接外部DLL;分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建一个mex文件,它与一个外部C ++库与一些硬件进行通信的MATLAB接口。硬件供应商为我的VC ++版本提供了导入的库和预编译的DLL(.lib和.dll),我可以在C ++中实现它们,而没有任何问题。



但是,当代码写为mex(使用相同版本的VC ++编译)时,我在运行时遇到分段错误。经过对VC ++调试器的一些调查,可能的罪魁祸首似乎是这样的事实:外部dll函数之一返回数据类型std :: vector,并且可能试图为函数内部的向量容器动态分配内存。我知道,如果我在我自己的mex函数中使用std :: vector,一切工作正常,但我怀疑mex头本身包装std :: vector容器在我自己的代码内存管理(?)根据需要所有动态分配的内存在mex代码,而它不能做同样的预编译的.dll。



现在的问题是:因为我不能修改外部.dll文件,没有访问其源文件,有什么办法使用这个外部DLL,使动态内存变得由MATLAB(可能是某种类型的包装器...?)管理,从而避免了分割错误和返回正确的数据?或者如果我的分析错误,请更正我!



请让我知道是否有任何想法或黑客,谢谢!



我的系统:Windows 7 SP1 32位,MATLAB 2009b,Visual C ++ 2008 Pro。



我也发布了同样的问题:
< a href =http://www.mathworks.com/matlabcentral/answers/9294-mex-dynamic-memory-management-issue-with-std-vector-in-linked-external-dll-segmentation-error = nofollow> http://www.mathworks.com/matlabcentral/answers/9294-mex-dynamic-memory-management-issue-with-std-vector-in-linked-external-dll-segmentation-error

解决方案

感谢大家的答案和意见。我能够在MathWorks的友好人士的帮助下解决这个问题。



http://www.mathworks。 com / matlabcentral / answers / 9294-mex-dynamic-memory-management-issue-with-std-vector-in-linked-external-dll-segmentation-error


您可能看到stl库和/或编译器选项与预编译的dll和MATLAB和MEX命令使用的选项不兼容。 MATLAB 2009b使用MSVC 2005构建。



您可以通过更改mex使用的选项或直接使用MSVC构建您的mex文件来解决问题。可能影响事物的选项的一个示例是SECURE_SCL = 0。我将开始建立你的测试程序与选项MATLAB是使用找到有问题的选项,然后尝试删除该选项,当建立mex文件。



由于这种不匹配的使用stl对象在api的第三方编译库通常是一个坏主意。


按照他的建议,我删除了SECURE_SCL = 0选项从

$ me $选项文件

C:\Users \(username)\AppData\Roaming\MathWorks\MATLAB\R2009b\\ \\ mexopts.bat



然后重新编译mex文件,现在一切都像一个charm - 函数返回正确的数据,并且分段错误不再出现。


I am trying to create a mex file that interfaces MATLAB with an external C++ library that communicates with some hardware. An imported library and precompiled DLL (.lib and .dll) are provided by the hardware vendor for my version of VC++ and I was able to implement them in C++ without any issue.

However, I ran into segmentation error at run time when the code is written as a mex(compiled with the same version of VC++). After some investigation with the VC++ debugger, the likely culprit seems to be the fact that one of the external dll functions returns the data type std::vector, and probably tries to dynamically allocate memory for the vector container somewhere inside the function. I know that if I use std::vector in my own mex function, everything works fine, but I suspect that the mex header itself wraps the std::vector container in my own code for memory management(?) as required for all dynamically allocated memory in mex codes, whereas it can't do the same for the pre-compiled .dll.

Now the question is: since I cannot modify the external .dll file and have no access to its source files, are there any ways to work with this external dll such that the dynamic memory becomes managed by MATLAB(perhaps a wrapper of some sort..?)...and thereby avoid the segmentation error and return the correct data? Or if my analysis is wrong please correct me too!

Please let me know if there are any ideas or hacks, thanks!

My system: Windows 7 SP1 32 bit, MATLAB 2009b, Visual C++ 2008 Pro.

I also posted the same question at: http://www.mathworks.com/matlabcentral/answers/9294-mex-dynamic-memory-management-issue-with-std-vector-in-linked-external-dll-segmentation-error .You also can share your insights there if you have an account, thanks!

解决方案

Thanks everyone for the answers and comments. I was able to resolve the issue with some help from the friendly folks at MathWorks.

From the original post at http://www.mathworks.com/matlabcentral/answers/9294-mex-dynamic-memory-management-issue-with-std-vector-in-linked-external-dll-segmentation-error :

You are probably seeing an incompatibility between the stl library and or compiler options used by your pre-compiled dll and those used by MATLAB and the MEX command. MATLAB 2009b was built with MSVC 2005.

You may be able to fix the problem by changing the options used by mex or by building your mex file directly with MSVC. One example of an option that may effect things is SECURE_SCL=0. I would start by building your test program with the options MATLAB is using to find the problematic option then try removing that option when building the mex file.

Because of this sort of incompatibility use of stl objects in the api's of third party compiled libraries is usually a bad idea.

Following his advice, I removed the SECURE_SCL=0 option from the mex options file at

C:\Users\(username)\AppData\Roaming\MathWorks\MATLAB\R2009b\mexopts.bat

Then recompiled the mex file, now everything works like a charm - the function is returning the correct data and segmentation error no longer occurs.

这篇关于Mex动态内存管理问题与std :: vector在链接外部DLL;分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆