我陷入了用python包装C dll的困境。 dll一侧提供了黑匣子,我另外做了包装器类。 python给C dll numpy ndarray。然后C dll处理它并返回它。
当我用C删除数组时,整个程序死亡。有或没有深层复制。下面是要复制的简短代码
python main
import mypyd,numpy
myobject = mypyd.myimpro()
image = readimg() #ndarray
result = myobject.process(image) <- process die here with exit code -1073741819
dllmain标头
BOOST_PYTHON_MODULE(mypyd){
class_<mywrapclass>("myimpro")
.def("process",&mywrapclass::process)
}
c包装标头
namespace np = boost::python::numpy;
class mywrapclass{
dllhandler m_handler;
myimagestruct process(np::ndarray);
}
c包装器代码
mywrapclass::mywrapclass(){
Py_Initialize();
np::initialize();
m_handler = initInDLL()
}
np::ndarray mywrapclass::process(np::ndarray input){
myimagestruct image = ndarray2struct(input); ## deep copy data array
myimagestruct result = processInDLL(m_handler,image);
np::ndarray ret = struct2ndarray(result); ## also deep copy data array. just in case.
return ret;
}
dll的c标头(我无法修改此代码。它由dll提供)
typedef struct {
void *data
... size,type etc
} myimagestruct;
typedef void * dllhandler;
dllhandler = initInDLL();
myimagestruct processInDLL(myimagestruct);
对不起,代码错误。最初,我天真地认为boost.python或深度复制将解决此堆内存问题,哈哈。尝试了大脑中几乎所有的关键字,但甚至找不到起点。
dll完美运行而无需增强包装。因此,解决它必须是我的一部分。 仍然感谢您的阅读。