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++ 中的 class 有成员 objects,它们是指向其他 classes、boost::unordered_map
的指针以及具有指针成员的结构。 class 也有模板函数,签名为 static void *(void * )
的函数。在其中一个 header 文件中,我声明了 extern const
objects ,它们也被定义了。 header 看起来像:
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
的全局对象是问题所在。不包括全局对象现在解决了错误。
我正在尝试用 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++ 中的 class 有成员 objects,它们是指向其他 classes、boost::unordered_map
的指针以及具有指针成员的结构。 class 也有模板函数,签名为 static void *(void * )
的函数。在其中一个 header 文件中,我声明了 extern const
objects ,它们也被定义了。 header 看起来像:
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)
基于
--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
的全局对象是问题所在。不包括全局对象现在解决了错误。