I'm using the following code PyObject * get_memoryview (PyObject *self) { Py_buffer view; ... // this takes a ref on self if (PyBuffer_FillInfo (&view, self, buffer, length, 0, 0) < 0) return NULL; // this returns a object return PyMemoryView_FromBuffer (&view); } The problem is that when I call release() on the returned memory object the buffer does not get release and as a result the exporter leaks. Am I missing something or is this a bug?
"Py_buffer view" needs to go into the exporting object. That object needs a getbufferproc(). That getbufferproc() can use PyBuffer_FillInfo() to fill in the view that is now kept alive because it's in the object. The object then supports the buffer protocol and memoryviews can be created automatically from the Python level without any further C code.
Ah, so if I understand correctly, exposing always requires something implementing the buffer interface in some way. I might require multiple different memoryviews for this object, so maybe some proxy object implementing it might work. Thanks Stefan for your quick help.
Sorry, actually I wasn't totally clear: The exporting object just needs a getbufferproc() that can respond to buffer requests and keeps track of how many views are exported. "View in the object" is misleading and not necessary; it can be used for some advanced stuff.