繁体   English   中英

boost.python模块扩展生成SIGSEGV

[英]boost.python module extension generates SIGSEGV

我正在尝试在C ++中开发python扩展。 我将使用boost.python构建模块。

我已经用clang编译了boost1.69.0

Apple LLVM version 10.0.0 (clang-1000.11.45.5)
Target: x86_64-apple-darwin17.7.0
Thread model: posix

我在C ++中的类具有成员对象,这些对象是指向其他类的指针, boost::unordered_map以及具有指针成员的结构。 该类还具有模板化函数,这些函数具有签名static void *(void * ) 在一个头文件中,我声明了也定义的extern const对象。 标题看起来像:

my_class.hpp:

#include <otherclass.hpp>
#include <boost/python.hpp>

class DP{

// Class objects and methods defined here
};

#ifdef __cplusplus
extern "C" {
#endif
    DP * newcase(const char *dir, int lenpath);
#ifdef __cplusplus
}
#endif

my_class.cpp:

DP * newcase(const char *dir, int lenpath){
    std::string path(dir);
    path = path.substr(0, lenpath);
    fs::path runDir(path);
    return new DP(runDir);
}

BOOST_PYTHON_MODULE(dp_py)
{
    using namespace boost::python;
    class_<DP>("DP", init<const char *>());

    def("newcase", &newcase, return_value_policy<manage_new_object>());
}

编译和链接也可以。 此外,我能够与另一个Fortran90项目进行接口和使用而不会出现任何问题。 但是,当我尝试在Python中导入相同的内容时,

>>> from ctypes import *
>>> from os import environ, listdir
>>> import dp_py

我遇到细分错误

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

基于答案,我尝试使用python调试器。 最后,我看到以下输出:

--Call--
> <frozen importlib._bootstrap_external>(919)create_module()
(Pdb) 
> <frozen importlib._bootstrap_external>(921)create_module()
(Pdb) 
> <frozen importlib._bootstrap_external>(922)create_module()
(Pdb) 
--Call--
> <frozen importlib._bootstrap>(211)_call_with_frames_removed()
(Pdb) 
> <frozen importlib._bootstrap>(219)_call_with_frames_removed()
(Pdb) 

Segmentation fault: 11

<frozen importlib._bootstrap>(219)_call_with_frames_removed()中执行的操作会导致分段错误吗? 我该如何解决?

我使用的python解释器是:

Python 3.6.8 :: Anaconda custom (64-bit)

事实证明,使用extern const包含全局对象是问题所在。 现在不包括全局对象可以解决该错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM