Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Segfault with Boost Python on cleanup

I am trying to follow some simple examples using libboost-python3 to pass a C++ structure to 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:

# 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:

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()
like image 938
Samuel O'Malley Avatar asked Jan 26 '26 00:01

Samuel O'Malley


1 Answers

Here someone got the same bug. He was using ld linker with g++ compiler. Using g++ linker solved his problem. You should check your linker in CMakeFiles/boost_python_exemplar.dir/link.txt.

like image 128
cromod Avatar answered Jan 27 '26 14:01

cromod



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!