简体   繁体   English

Segfault使用Boost Python进行清理

[英]Segfault with Boost Python on cleanup

I am trying to follow some simple examples using libboost-python3 to pass a C++ structure to Python. 我试图用一些简单的例子来使用libboost-python3将C ++结构传递给Python。 The functionality works as expected, however it segfaults on exit. 该功能按预期工作,但在退出时会出现段错误。

I have distilled this down to the simplest example and I am still getting a segfault when the created object is deallocated. 我已经将其提炼到最简单的示例,并且当创建的对象被释放时,我仍然会得到段错误。

/* boost_python_exemplar.cpp: */

#include <boost/python.hpp>

struct Test_Struct
{
  int a;
};

BOOST_PYTHON_MODULE(libboost_python_exemplar)
{
  using namespace boost::python;

  class_<Test_Struct>("Test_Struct")
    .def_readwrite("a", &Test_Struct::a);
}

Then the accompanying python code: 然后是随附的python代码:

# test.py

import libboost_python_exemplar
d = libboost_python_exemplar.Test_Struct()
# Segfault occurs here when the import is being cleaned up

Do I need to be using ref-counting or do I have to do an explicit cleanup step? 我是否需要使用引用计数或者是否必须执行明确的清理步骤? I am struggling to find anything wrong with this example seeing as it is so simple. 我正在努力找到这个例子的任何错误,因为它很简单。

Also the accompanying CMakeLists.txt file: 附带的CMakeLists.txt文件:

cmake_minimum_required(VERSION 3.3)
project(Boost_Python_Exemplar)

SET(Boost_INCLUDE_DIR /usr/local/boost/1.55.0/include/)
SET(Boost_LIBRARY_DIR /usr/local/boost/1.55.0/lib64/)
FIND_PACKAGE(Boost 1.55)
IF(Boost_FOUND)
  INCLUDE_DIRECTORIES("${Boost_INCLUDE_DIRS}" "/usr/local/anaconda_py3/include/python3.4m/")
  SET(Boost_USE_STATIC_LIBS_OFF)
  SET(Boost_USE_MULTITHREADED ON)
  SET(Boost_USE_STATIC_RUNTIME OFF)
  FIND_PACKAGE(Boost 1.55 COMPONENTS python3 REQUIRED)

  ADD_LIBRARY(boost_python_exemplar SHARED boost_python_exemplar.cpp)
  TARGET_LINK_LIBRARIES(boost_python_exemplar ${Boost_LIBRARIES})
ELSE()
  MESSAGE(FATAL_ERROR "Unable to find correct Boost version.")
ENDIF()

IF(CMAKE_COMPILER_IS_GNUCXX)
  ADD_DEFINITIONS("-Wall" "-pedantic" "-g")
ELSE()
  MESSAGE(FATAL_ERROR "CMakeLists.txt requires GCC")
ENDIF()

Here someone got the same bug. 这里有人得到了同样的错误。 He was using ld linker with g++ compiler. 他正在使用带有g ++编译器的ld链接器。 Using g++ linker solved his problem. 使用g ++链接器解决了他的问题。 You should check your linker in CMakeFiles/boost_python_exemplar.dir/link.txt. 您应该在CMakeFiles / boost_python_exemplar.dir / link.txt中检查链接器。

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

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