[英]Python Ctypes Crashes Calling C wrapper of C++ function
I am trying to use Python Ctypes to interface a published (closed source) C++ library. 我正在尝试使用Python Ctypes来连接已发布的(闭源)C ++库。 I (tried) wrote a basic C style function wrapper to construct the C++ vector style objects and call the C++ routine. 我(试过)编写了一个基本的C风格函数包装器来构造C ++向量样式对象并调用C ++例程。 I also (tried) wrote a basic python script to load the shared library. 我也(试过)写了一个基本的python脚本来加载共享库。 Everything is working except the line that calls the C++ routine which yields: 一切正常,除了调用C ++例程的行,产生:
*** glibc detected *** python: free(): invalid next size (fast): 0x0000000001e73c00 ***
Here are the files, unfortunately I cannot share headers, but I may be able to write something similar if need be... 以下是文件,遗憾的是我无法共享标题,但如果需要,我可能会写类似的内容......
gaumixmod.cpp: gaumixmod.cpp:
#include "nr3.h"
#include "cholesky.h"
#include "gaumixmod.h"
extern "C" {
void cGaumixmod(double* D, int Dm, int Dn, double* M, int Mm, int Mn) {
MatDoub ddata(Dm,Dn,*D); // construct Matrix (vector) type
MatDoub mmeans(Mm,Mn,*M); // construct Matrix (vector) type
//XXX test numpy array is coming through as C array and we can rw, checks OK
int i;
// for(i=0;i<Dn*Dm;++i) {
// printf("Address %x : ",(D+i));
// printf("was %f \t" , D[i]);
// D[i]+=1.0;
// printf("now: %f \n" , D[i]);
// }
// check that array D was copied to matrix ddata, and we can r/w
for(i=0;i<Dm*Dn;++i) {
printf("iter %d Address %x : ",i,ddata[i/Dm][i%Dm]);
printf("was %f \t" , ddata[i/Dm][i%Dm]);
ddata[i/Dm][i%Dm]+=1.0;
printf("now: %f \n" ,ddata[i/Dm][i%Dm]);
}
Gaumixmod::Gaumixmod(ddata,mmeans);
//return data from vector to ctypes array C so we can check data returns to python
//via numpy array, checks ok
for(i=0;i<Dm*Dn;++i) {
D[i] = ddata[i/Dm][i%Dm];
}
}
}
goumixmod.py: goumixmod.py:
import platform,ctypes
import numpy as np
# ------------------------------------------------------------------------
# define correct library from platfrom, assuming 64bit for linux machines
# ------------------------------------------------------------------------
if platform.system()=='Microsoft':
raise Exception('MS not supported.')
elif platform.system()=='Darwin':
libgaumixmod = ctypes.cdll.LoadLibrary("./gaumixmod.so")
elif platform.system()=='Linux':
libgaumixmod = ctypes.cdll.LoadLibrary("./gaumixmod.so")
else:
#hope for the best
libgaumixmod = ctypes.cdll.LoadLibrary("./gaumixmod.so")
# --------------------------------------------------
# define SafeCall
#---------------------------------------------------
def SafeCall(ret):
"""pass, code l8r""
print ret
#---------------------------------------------------
# define arg types and res types of function
# -----------------------------------------------------------------------
_gaumixmod = libgaumixmod.cGaumixmod
_gaumixmod.restype = ctypes.c_int
_gaumixmod.argtypes = [np.ctypeslib.ndpointer(dtype=np.float64,flags='C_CONTIGUOUS'),
ctypes.c_int,
ctypes.c_int,
np.ctypeslib.ndpointer(dtype=np.float64,flags='C_CONTIGUOUS'),
ctypes.c_int,
ctypes.c_int]
def gaumixmod(D,K):
"""Python binding for C++ guassian mixure model code."""
ret = _gaumixmod(D,D.shape[0],D.shape[1],K,K.shape[0],K.shape[1])
SafeCall(ret)
return D,K
D = np.ones((100,100)).astype(np.float64)
K = np.ones((4,1)).astype(np.float64)
print gaumixmod(D,K)
and I compile this jazz with: 我用以下内容编译这个爵士乐:
g++ -fPIC -c gaumixmod.cpp;
g++ -shared -o gaumixmod.so gaumixmod.o
and run 并运行
python gaumixmod.py
My research indicates this error is something akin to a segFault, where python is trying to reach a memory outside its scope... This is the part I don't understand because commenting out the C++ line Gaumixmod::Gaumixmod(), everything works fine, and that routine should be operating on the vectors instantiated in the cGaumixmod() function, not the python numpy arrays. 我的研究表明这个错误类似于segFault,其中python试图到达其范围之外的内存......这是我不理解的部分,因为注释掉C ++行Gaumixmod :: Gaumixmod(),一切正常很好,那个例程应该在cGaumixmod()函数中实例化的向量上运行,而不是python numpy数组。 I am really unfamiliar with C++, although I have used C types many times for C libraries. 我真的不熟悉C ++,虽然我已经多次使用C类型的C库。 I am hoping that someone with some C++,python,and ctypes experience can give some insight/guidance here. 我希望拥有一些C ++,python和ctypes经验的人可以在这里提供一些见解/指导。
Thanks! 谢谢!
There is a new library for interfacing with C, you might what to look at it: 有一个用于与C接口的新库,您可以查看它:
http://cffi.readthedocs.org/en/latest/index.html http://cffi.readthedocs.org/en/latest/index.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.