Python C类型分段/总线错误
编辑:Python C类型分段/总线错误,python,c++,macos,c++14,ctypes,Python,C++,Macos,C++14,Ctypes,编辑: detector_new用于返回新检测器()并且它经常失败,可能失败了3/5次 下面的代码失败的次数要少得多,可能是1/20次 如果我把地址还给这个,它似乎永远不会失败 Detector _this; extern "C" { void *detector_new() { return new Detector; // return &_this; } 原始问题: 我的OSXC++/Python模拟代码有时有效,有时会生
detector_new
用于返回新检测器()代码>并且它经常失败,可能失败了3/5次
下面的代码失败的次数要少得多,可能是1/20次
如果我把地址还给这个,它似乎永远不会失败
Detector _this;
extern "C"
{
void *detector_new()
{
return new Detector;
// return &_this;
}
原始问题:
我的OSXC++/Python模拟代码有时有效,有时会生成分段错误和总线错误。我尝试过为ctypes
仔细定义参数和返回类型。我的应用程序类型如U16
映射到
中的相应值
似乎不喜欢这个C++类方法:
void考虑(u16a、u16b、u16c);
对于动态库,它的包装如下所示
使用检测器p=检测器*;
空隙检测器(空隙*d、U16 a、U16 b、U16 c)
{
检测器P(d)->考虑(a,b,c);
}
…并在Python中使用ctypes
定义为:
\u library=cdll.LoadLibrary('libhail.dylib')
f=\u library.detector\u考虑
f、 重新类型=无
f、 argtypes=[c_voidp,c_ushort,c_ushort,c_ushort]
我以这种方式建立图书馆:
#/bin/bash
clang++-c-g-DSIMULATION-I.-I../Application-I../Application/deposition Interface.cpp../Application/deposition/Hail.cpp-std=c++14
clang++-o libhail.dylib-dynamiclib Hail.o Interface.o