<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>The problem doesn't have anything to do with boost.python. The thing is in initializing numpy API pointer specifically PyArray_API*. If you print this pointer before creating the array, it'll turn out to be null. If you move import_array function just before the array creation call, it should work. You can find more details here <br><a href="http://docs.scipy.org/doc/numpy/reference/c-api.array.html">http://docs.scipy.org/doc/numpy/reference/c-api.array.html</a> in Miscellaneous section at the bottom. </div><div><br>Regards,</div><div>Alexandra</div><div><br>05 авг. 2016 г., в 20:00, <a href="mailto:cplusplus-sig-request@python.org">cplusplus-sig-request@python.org</a> написал(а):<br><br></div><blockquote type="cite"><div><span>Send Cplusplus-sig mailing list submissions to</span><br><span>    <a href="mailto:cplusplus-sig@python.org">cplusplus-sig@python.org</a></span><br><span></span><br><span>To subscribe or unsubscribe via the World Wide Web, visit</span><br><span>    <a href="https://mail.python.org/mailman/listinfo/cplusplus-sig">https://mail.python.org/mailman/listinfo/cplusplus-sig</a></span><br><span>or, via email, send a message with subject or body 'help' to</span><br><span>    <a href="mailto:cplusplus-sig-request@python.org">cplusplus-sig-request@python.org</a></span><br><span></span><br><span>You can reach the person managing the list at</span><br><span>    <a href="mailto:cplusplus-sig-owner@python.org">cplusplus-sig-owner@python.org</a></span><br><span></span><br><span>When replying, please edit your Subject line so it is more specific</span><br><span>than "Re: Contents of Cplusplus-sig digest..."</span><br><span></span><br><span></span><br><span>Today's Topics:</span><br><span></span><br><span>   1. Segmentation Fault (core dumped) on Python 3.5.2 but not</span><br><span>      Python 2.7.12 (Matthew Conte)</span><br><span>   2. Re: Segmentation Fault (core dumped) on Python 3.5.2 but not</span><br><span>      Python 2.7.12 (Stefan Seefeld)</span><br><span>   3. Re: Segmentation Fault (core dumped) on Python 3.5.2 but not</span><br><span>      Python 2.7.12 (Matthew Conte)</span><br><span></span><br><span></span><br><span>----------------------------------------------------------------------</span><br><span></span><br><span>Message: 1</span><br><span>Date: Thu, 4 Aug 2016 13:10:23 -0400</span><br><span>From: Matthew Conte <<a href="mailto:matt.p.conte@gmail.com">matt.p.conte@gmail.com</a>></span><br><span>To: <a href="mailto:cplusplus-sig@python.org">cplusplus-sig@python.org</a></span><br><span>Subject: [C++-sig] Segmentation Fault (core dumped) on Python 3.5.2</span><br><span>    but not Python 2.7.12</span><br><span>Message-ID:</span><br><span>    <<a href="mailto:CAFwmYYjxXXVQV=6Wv8x_q4Uc2GjogR1OR_JSURhP3kCMrEb-gQ@mail.gmail.com">CAFwmYYjxXXVQV=6Wv8x_q4Uc2GjogR1OR_JSURhP3kCMrEb-gQ@mail.gmail.com</a>></span><br><span>Content-Type: text/plain; charset="utf-8"</span><br><span></span><br><span>So I'm trying to create a boost python module that simply creates and</span><br><span>returns a numpy array,</span><br><span>but the function crashes (sometimes) and it doesn't ever seem to crash on</span><br><span>Python 2.</span><br><span></span><br><span>Here's the source code I made:</span><br><span></span><br><span>#include <boost/python.hpp></span><br><span>#include <numpy/ndarrayobject.h></span><br><span></span><br><span>using namespace boost::python;</span><br><span></span><br><span>object create_numpy_array() {</span><br><span>npy_intp dims = 1;</span><br><span>long* data = new long[1];</span><br><span>data[0] = 1;</span><br><span>PyObject* obj = PyArray_SimpleNewFromData(1, &dims, PyArray_LONGLTR, data);</span><br><span>boost::python::handle<> handle(obj);</span><br><span>boost::python::numeric::array arr(handle);</span><br><span>return arr.copy();</span><br><span>}</span><br><span></span><br><span>BOOST_PYTHON_MODULE(create) {</span><br><span>import_array();</span><br><span>numeric::array::set_module_and_type("numpy", "ndarray");</span><br><span>def("numpy_array", &create_numpy_array);</span><br><span>}</span><br><span></span><br><span></span><br><span>using a simple python script to test:</span><br><span></span><br><span>import create</span><br><span>print(create.numpy_array())</span><br><span></span><br><span></span><br><span>The stack trace indicates that the crash occurs on a boost::python::handle</span><br><span>destructor trying to decrease the ref count of a PyObject with a ref count</span><br><span>of 0.</span><br><span></span><br><span>I've tried this on both Windows 7 and Ubuntu 16.04 both 64-bit.</span><br><span></span><br><span>Thank you.</span><br><span>-------------- next part --------------</span><br><span>An HTML attachment was scrubbed...</span><br><span>URL: <<a href="http://mail.python.org/pipermail/cplusplus-sig/attachments/20160804/2ad0a9ec/attachment-0001.html">http://mail.python.org/pipermail/cplusplus-sig/attachments/20160804/2ad0a9ec/attachment-0001.html</a>></span><br><span></span><br><span>------------------------------</span><br><span></span><br><span>Message: 2</span><br><span>Date: Thu, 4 Aug 2016 13:14:58 -0400</span><br><span>From: Stefan Seefeld <<a href="mailto:stefan@seefeld.name">stefan@seefeld.name</a>></span><br><span>To: <a href="mailto:cplusplus-sig@python.org">cplusplus-sig@python.org</a></span><br><span>Subject: Re: [C++-sig] Segmentation Fault (core dumped) on Python</span><br><span>    3.5.2 but not Python 2.7.12</span><br><span>Message-ID: <<a href="mailto:32f9c08d-2d65-c1b5-ca1a-1055f3784240@seefeld.name">32f9c08d-2d65-c1b5-ca1a-1055f3784240@seefeld.name</a>></span><br><span>Content-Type: text/plain; charset=windows-1252</span><br><span></span><br><span>Hi Matthew,</span><br><span></span><br><span>would you mind submitting an issue on</span><br><span><a href="https://github.com/boostorg/python/issues">https://github.com/boostorg/python/issues</a> for this.</span><br><span></span><br><span>Thanks,</span><br><span>        Stefan</span><br><span></span><br><span>On 04.08.2016 13:10, Matthew Conte wrote:</span><br><blockquote type="cite"><span>So I'm trying to create a boost python module that simply creates and</span><br></blockquote><blockquote type="cite"><span>returns a numpy array, </span><br></blockquote><blockquote type="cite"><span>but the function crashes (sometimes) and it doesn't ever seem to crash</span><br></blockquote><blockquote type="cite"><span>on Python 2.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Here's the source code I made:</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>#include <boost/python.hpp></span><br></blockquote><blockquote type="cite"><span>#include <numpy/ndarrayobject.h></span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>using namespace boost::python;</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>object create_numpy_array() {</span><br></blockquote><blockquote type="cite"><span>npy_intp dims = 1;</span><br></blockquote><blockquote type="cite"><span>long* data = new long[1];</span><br></blockquote><blockquote type="cite"><span>data[0] = 1;</span><br></blockquote><blockquote type="cite"><span>PyObject* obj = PyArray_SimpleNewFromData(1, &dims, PyArray_LONGLTR,</span><br></blockquote><blockquote type="cite"><span>data);</span><br></blockquote><blockquote type="cite"><span>boost::python::handle<> handle(obj);</span><br></blockquote><blockquote type="cite"><span>boost::python::numeric::array arr(handle);</span><br></blockquote><blockquote type="cite"><span>return arr.copy();</span><br></blockquote><blockquote type="cite"><span>}</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>BOOST_PYTHON_MODULE(create) {</span><br></blockquote><blockquote type="cite"><span>import_array();</span><br></blockquote><blockquote type="cite"><span>numeric::array::set_module_and_type("numpy", "ndarray");</span><br></blockquote><blockquote type="cite"><span>def("numpy_array", &create_numpy_array);</span><br></blockquote><blockquote type="cite"><span>}</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>using a simple python script to test:</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>import create</span><br></blockquote><blockquote type="cite"><span>print(create.numpy_array())</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>The stack trace indicates that the crash occurs on a</span><br></blockquote><blockquote type="cite"><span>boost::python::handle destructor trying to decrease the ref count of a</span><br></blockquote><blockquote type="cite"><span>PyObject with a ref count of 0.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>I've tried this on both Windows 7 and Ubuntu 16.04 both 64-bit.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Thank you.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>_______________________________________________</span><br></blockquote><blockquote type="cite"><span>Cplusplus-sig mailing list</span><br></blockquote><blockquote type="cite"><span><a href="mailto:Cplusplus-sig@python.org">Cplusplus-sig@python.org</a></span><br></blockquote><blockquote type="cite"><span><a href="https://mail.python.org/mailman/listinfo/cplusplus-sig">https://mail.python.org/mailman/listinfo/cplusplus-sig</a></span><br></blockquote><span></span><br><span></span><br><span>-- </span><br><span></span><br><span>      ...ich hab' noch einen Koffer in Berlin...</span><br><span></span><br><span></span><br><span></span><br><span>------------------------------</span><br><span></span><br><span>Message: 3</span><br><span>Date: Thu, 4 Aug 2016 13:16:58 -0400</span><br><span>From: Matthew Conte <<a href="mailto:matt.p.conte@gmail.com">matt.p.conte@gmail.com</a>></span><br><span>To: "Development of Python/C++ integration" <<a href="mailto:cplusplus-sig@python.org">cplusplus-sig@python.org</a>></span><br><span>Subject: Re: [C++-sig] Segmentation Fault (core dumped) on Python</span><br><span>    3.5.2 but not Python 2.7.12</span><br><span>Message-ID:</span><br><span>    <<a href="mailto:CAFwmYYjN4CDP4sLAs0C4JACFHPOxLeDyPX+LoSpv4NdGVfhGEQ@mail.gmail.com">CAFwmYYjN4CDP4sLAs0C4JACFHPOxLeDyPX+LoSpv4NdGVfhGEQ@mail.gmail.com</a>></span><br><span>Content-Type: text/plain; charset="utf-8"</span><br><span></span><br><span>I will, thank you.</span><br><span></span><br><span>On Thu, Aug 4, 2016 at 1:14 PM, Stefan Seefeld <<a href="mailto:stefan@seefeld.name">stefan@seefeld.name</a>> wrote:</span><br><span></span><br><blockquote type="cite"><span>Hi Matthew,</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>would you mind submitting an issue on</span><br></blockquote><blockquote type="cite"><span><a href="https://github.com/boostorg/python/issues">https://github.com/boostorg/python/issues</a> for this.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Thanks,</span><br></blockquote><blockquote type="cite"><span>        Stefan</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>On 04.08.2016 13:10, Matthew Conte wrote:</span><br></blockquote><blockquote type="cite"><blockquote type="cite"><span>So I'm trying to create a boost python module that simply creates and</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>returns a numpy array,</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>but the function crashes (sometimes) and it doesn't ever seem to crash</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>on Python 2.</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>Here's the source code I made:</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>#include <boost/python.hpp></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>#include <numpy/ndarrayobject.h></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>using namespace boost::python;</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>object create_numpy_array() {</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>npy_intp dims = 1;</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>long* data = new long[1];</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>data[0] = 1;</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>PyObject* obj = PyArray_SimpleNewFromData(1, &dims, PyArray_LONGLTR,</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>data);</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>boost::python::handle<> handle(obj);</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>boost::python::numeric::array arr(handle);</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>return arr.copy();</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>}</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>BOOST_PYTHON_MODULE(create) {</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>import_array();</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>numeric::array::set_module_and_type("numpy", "ndarray");</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>def("numpy_array", &create_numpy_array);</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>}</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>using a simple python script to test:</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>import create</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>print(create.numpy_array())</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>The stack trace indicates that the crash occurs on a</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>boost::python::handle destructor trying to decrease the ref count of a</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>PyObject with a ref count of 0.</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>I've tried this on both Windows 7 and Ubuntu 16.04 both 64-bit.</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>Thank you.</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>_______________________________________________</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>Cplusplus-sig mailing list</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span><a href="mailto:Cplusplus-sig@python.org">Cplusplus-sig@python.org</a></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span><a href="https://mail.python.org/mailman/listinfo/cplusplus-sig">https://mail.python.org/mailman/listinfo/cplusplus-sig</a></span><br></blockquote></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>--</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>      ...ich hab' noch einen Koffer in Berlin...</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>_______________________________________________</span><br></blockquote><blockquote type="cite"><span>Cplusplus-sig mailing list</span><br></blockquote><blockquote type="cite"><span><a href="mailto:Cplusplus-sig@python.org">Cplusplus-sig@python.org</a></span><br></blockquote><blockquote type="cite"><span><a href="https://mail.python.org/mailman/listinfo/cplusplus-sig">https://mail.python.org/mailman/listinfo/cplusplus-sig</a></span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><span>-------------- next part --------------</span><br><span>An HTML attachment was scrubbed...</span><br><span>URL: <<a href="http://mail.python.org/pipermail/cplusplus-sig/attachments/20160804/b50c011c/attachment-0001.html">http://mail.python.org/pipermail/cplusplus-sig/attachments/20160804/b50c011c/attachment-0001.html</a>></span><br><span></span><br><span>------------------------------</span><br><span></span><br><span>Subject: Digest Footer</span><br><span></span><br><span>_______________________________________________</span><br><span>Cplusplus-sig mailing list</span><br><span><a href="mailto:Cplusplus-sig@python.org">Cplusplus-sig@python.org</a></span><br><span><a href="https://mail.python.org/mailman/listinfo/cplusplus-sig">https://mail.python.org/mailman/listinfo/cplusplus-sig</a></span><br><span></span><br><span>------------------------------</span><br><span></span><br><span>End of Cplusplus-sig Digest, Vol 94, Issue 2</span><br><span>********************************************</span><br></div></blockquote></body></html>