[C++-sig] Segmentation Fault (core dumped) on Python 3.5.2 but not Python 2.7.12

Matthew Conte matt.p.conte at gmail.com
Thu Aug 4 13:10:23 EDT 2016


So I'm trying to create a boost python module that simply creates and
returns a numpy array,
but the function crashes (sometimes) and it doesn't ever seem to crash on
Python 2.

Here's the source code I made:

#include <boost/python.hpp>
#include <numpy/ndarrayobject.h>

using namespace boost::python;

object create_numpy_array() {
npy_intp dims = 1;
long* data = new long[1];
data[0] = 1;
PyObject* obj = PyArray_SimpleNewFromData(1, &dims, PyArray_LONGLTR, data);
boost::python::handle<> handle(obj);
boost::python::numeric::array arr(handle);
return arr.copy();
}

BOOST_PYTHON_MODULE(create) {
import_array();
numeric::array::set_module_and_type("numpy", "ndarray");
def("numpy_array", &create_numpy_array);
}


using a simple python script to test:

import create
print(create.numpy_array())


The stack trace indicates that the crash occurs on a boost::python::handle
destructor trying to decrease the ref count of a PyObject with a ref count
of 0.

I've tried this on both Windows 7 and Ubuntu 16.04 both 64-bit.

Thank you.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20160804/2ad0a9ec/attachment.html>


More information about the Cplusplus-sig mailing list