[Numpy-discussion] Unreliable crash when converting using numpy.asarray via C buffer interface

Friedrich Romstedt friedrichromstedt at gmail.com
Tue Jan 26 03:48:58 EST 2021


Hi,

This is with Python 3.8.2 64-bit and numpy 1.19.2 on Windows 10.  I'd
like to be able to convert some C++ extension type to a numpy array by
using ``numpy.asarray``.  The extension type implements the Python
buffer interface to support this.

The extension type, called "Image" here, holds some chunk of
``double``, C order, contiguous, 2 dimensions.  It "owns" the buffer;
the buffer is not shared with other objects.  The following Python
code crashes::

    image = <... Image production ...>
    ar = numpy.asarray(image)

However, when I say::

    image = <... Image production ...>
    print("---")
    ar = numpy.asarray(image)

the entire program is executing properly with correct data in the
numpy ndarray produced using the buffer interface.

The extension type permits reading the pixel values by a method;
copying them over by a Python loop works fine.  I am ``Py_INCREF``-ing
the producer in the C++ buffer view creation function properly.  The
shapes and strides of the buffer view are ``delete[]``-ed upon
releasing the buffer; avoiding this does not prevent the crash.  I am
catching ``std::exception`` in the view creation function; no such
exception occurs.  The shapes and strides are allocated by ``new
Py_ssize_t[2]``, so they will survive the view creation function.

I spent some hours trying to figure out what I am doing wrong.  Maybe
someone has an idea about this?  I double-checked each line of code
related to this problem and couldn't find any mistake.  Probabaly I am
not looking at the right aspect.

Best,
Friedrich


More information about the NumPy-Discussion mailing list