我正在尝试将 Python 2.6 嵌入到 MATLAB (7.12) 中。我想嵌入一个用 C 编写的 mex 文件。这对于使用标量的小型简单示例来说效果很好。但是,如果以任何方式导入 Numpy (1.6.1),MATLAB 都会崩溃。我说无论如何是因为我尝试了多种方法来加载 numpy 库,包括
-
在Python模块(.py)中:
from numpy import *
-
With PyRun_SimpleString
在 mex 文件中:
PyRun_SimpleString(“from numpy import *”);
-
调用 numpy 函数Py_oBject_CallObject
:
pOut = PyObject_CallObject(pFunc, pArgs);
最初,我认为这可能是在 C 中嵌入 Numpy 的问题。但是,当嵌入到使用 Visual Studios 2005 C 编译器使用 /MD(多线程)开关从命令行编译的简单 C 文件中时,Numpy 工作正常。接下来,我想我只需更改 MATLAB 中的 make 文件以包含 /MD 开关即可。没有这样的运气,mexopts.bat 使用 /MD 开关进行编译。我还手动注释掉了 Numpy init 模块中的行,以查找导致 MATLAB 崩溃的原因。加载任何扩展名为 pyd 的文件似乎都会导致 MATLAB 崩溃。 NumPy 中加载的第一个此类文件是 multiarray.pyd。 MATLAB 文档描述了如何使用 Visual Studio 调试 mex 文件,我这样做了并将错误消息放在下面。此时我知道问题是 pyd 的内存问题以及与 MATLAB 的一些冲突。有趣的是,我可以使用 MATLAB 中的系统命令来启动使用 numpy 的 python 进程,并且不会生成任何错误。我将在下面粘贴来自 MATLAB 的错误消息,然后粘贴 Visual Studio 中导致 MATLAB 崩溃的进程的 DEBUG 输出。然而,我不会粘贴整个内容,因为第一次机会例外的列表很长。对于解决这个集成问题有什么建议吗?
MATLAB error
Matlab has encountered an internal problem and needs to close
MATLAB crash file:C:\Users\pml355\AppData\Local\Temp\matlab_crash_dump.3484-1:
------------------------------------------------------------------------
Segmentation violation detected at Tue Oct 18 12:19:03 2011
------------------------------------------------------------------------
Configuration:
Crash Decoding : Disabled
Default Encoding: windows-1252
MATLAB License : 163857
MATLAB Root : C:\Program Files\MATLAB\R2011a
MATLAB Version : 7.12.0.635 (R2011a)
Operating System: Microsoft Windows 7
Processor ID : x86 Family 6 Model 7 Stepping 10, GenuineIntel
Virtual Machine : Java 1.6.0_17-b04 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode
Window System : Version 6.1 (Build 7600)
Fault Count: 1
Abnormal termination:
Segmentation violation
Register State (from fault):
EAX = 00000001 EBX = 69c38c20
ECX = 00000001 EDX = 24ae1da8
ESP = 0088af0c EBP = 0088af44
ESI = 69c38c20 EDI = 24ae1da0
EIP = 69b93d31 EFL = 00010202
CS = 0000001b DS = 00000023 SS = 00000023
ES = 00000023 FS = 0000003b GS = 00000000
Stack Trace (from fault):
[ 0] 0x69b93d31 C:/Python26/Lib/site-packages/numpy/core/multiarray.pyd+00081201 ( ???+000000 )
[ 1] 0x69bfead4 C:/Python26/Lib/site-packages/numpy/core/multiarray.pyd+00518868 ( ???+000000 )
[ 2] 0x69c08039 C:/Python26/Lib/site-packages/numpy/core/multiarray.pyd+00557113 ( ???+000000 )
[ 3] 0x08692b09 C:/Python26/python26.dll+00076553 ( PyEval_EvalFrameEx+007833 )
[ 4] 0x08690adf C:/Python26/python26.dll+00068319 ( PyEval_EvalCodeEx+002255 )
This error was detected while a MEX-file was running. If the MEX-file
is not an official MathWorks function, please examine its source code
for errors. Please consult the External Interfaces Guide for information
on debugging MEX-files.
If this problem is reproducible, please submit a Service Request via:
http://www.mathworks.com/support/contact_us/
A technical support engineer might contact you with further information.
Thank you for your help.
Visual Studio 调试器的输出
First-chance exception at 0x0c12c128 in MATLAB.exe: 0xC0000005: Access violation reading location 0x00000004.
First-chance exception at 0x0c12c128 in MATLAB.exe: 0xC0000005: Access violation reading location 0x00000004.
First-chance exception at 0x0c12c128 in MATLAB.exe: 0xC0000005: Access violation reading location 0x00000004.
First-chance exception at 0x751d9673 in MATLAB.exe: Microsoft C++ exception: jitCgFailedException at memory location 0x00c3e210..
First-chance exception at 0x751d9673 in MATLAB.exe: Microsoft C++ exception: jitCgFailedException at memory location 0x00c3e400..
First-chance exception at 0x69b93d31 in MATLAB.exe: 0xC0000005: Access violation writing location 0x00000001.
> throw_segv_longjmp_seh_filter()
throw_segv_longjmp_seh_filter(): invoking THROW_SEGV_LONGJMP SEH filter
> mnUnhandledWindowsExceptionFilter()
MATLAB.exe has triggered a breakpoint
尝试从Python方面解决问题:Python是一种很棒的粘合语言,我建议你让Python运行你的Matlab和C程序。 Python 有:
- Numpy
- PyLab
- Matplotlib
- IPython
因此,该组合是几乎所有现有 Matlab 模块的良好替代方案。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)