I want to allocate a buffer in C++ and provide it to python as a memoryview object. To do so, I expose this function using boost::python :
object* Allocator()
{
void* buff = my_alloc_function(char, size);
Py_buffer pybuffer;
int res = PyBuffer_FillInfo(&pybuffer, 0, buff, size, false, PyBUF_CONTIG);
if (res == -1)
return NULL;
handle<> obj_handle(borrowed(PyMemoryView_FromBuffer(&pybuffer)));
object obj = object(obj_handle);
return &obj;
}
I want also to be able to provide a C++ destructor called by python once the count ref of the python object goes to zero. I managed to have my destructor called by python and get the returned pointer by Allocator() as a parameter of my destructor.
Following, the currently C++ function called by python :
void Destructor()(object* pyMemoryView_object) const
{
Py_buffer* py_buffer = PyMemoryView_GET_BUFFER(pyMemoryView_object);
my_free_function(py_buffer->buf);
PyBuffer_Release(py_buffer);
}
Everything seems to go well until the destructor is called...
The problem I have is that the Py_buffer py_buffer
has a valid address but contains scrapped members (as shown as 0xcccccccc in debugger).
My first question is :
How to get back a valid Py_buffer object in C++ from python while this object has been constructed using handle<> and wrapped into a python object ?
My second question will be :
Do I have to call PyBuffer_Release() since it could be called by python after the call to Destructor() ?
Thanks for all your help !