链接外部 DLL 中 std::vector 的 Mex 动态内存管理问题;分割错误

2022-01-12 00:00:00 dll matlab segmentation-fault c++ mex

我正在尝试创建一个 mex 文件,该文件将 MATLAB 与与某些硬件通信的外部 C++ 库接口.硬件供应商为我的 VC++ 版本提供了一个导入的库和预编译的 DLL(.lib 和 .dll),我能够在 C++ 中毫无问题地实现它们.

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.

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

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.

现在的问题是:由于我无法修改外部 .dll 文件并且无法访问其源文件,是否有任何方法可以使用此外部 dll 以使动态内存由 MATLAB 管理(可能是某种......?)......从而避免分段错误并返回正确的数据?或者如果我的分析有误也请指正!

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!

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

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

我还在以下位置发布了相同的问题:http://www.mathworks.com/matlabcentral/answers/9294-mex-dynamic-memory-management-issue-with-std-vector-in-linked-external-dll-segmentation-error.如果您有帐户,也可以在那里分享您的见解,谢谢!

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!

推荐答案

感谢大家的回答和评论.在 MathWorks 友好人士的帮助下,我得以解决了这个问题.

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

来自 http://www.mathworks.com/matlabcentral/answers/9294-mex-dynamic-memory-management-issue-with-std-vector-in-linked-外部 dll 分段错误 :

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

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.

您可以通过更改 mex 使用的选项或直接使用 MSVC 构建 mex 文件来解决此问题.可能影响事物的选项的一个示例是 SECURE_SCL=0.我将首先使用 MATLAB 用来查找有问题的选项的选项构建您的测试程序,然后在构建 mex 文件时尝试删除该选项.

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.

由于这种不兼容,在第三方编译库的 api 中使用 stl 对象通常是个坏主意.

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

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

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

C:Users(用户名)AppDataRoamingMathWorksMATLABR2009bmexopts.bat

C:Users(username)AppDataRoamingMathWorksMATLABR2009bmexopts.bat

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

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

相关文章