On Mon, Feb 15, 2021 at 7:35 PM Mansour Moufid <mansourmou...@gmail.com> wrote: > > On Tue, Jan 26, 2021 at 3:50 AM Friedrich Romstedt > <friedrichromst...@gmail.com> wrote: > > > > 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. > > Maybe a dereference bug. > > Try setting pointers to NULL after freeing, something like this: > > delete[] view->shape; > view->shape = NULL; > delete[] view->strides; > view->strides = NULL; > > ... > > delete[] self->data; > self->data = NULL;
Sorry for two messages in a row, I just noticed: I don't see the type's tp_free member defined? You can set it to PyObject_Free in Init_ImageType: ImageType.tp_free = PyObject_Free; See here: https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_free _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion