sip - returning a MemoryView in %ConvertFromTypeCode
Scott Talbert
swt at techie.net
Sun Dec 26 21:47:50 GMT 2021
Hi,
I'm trying to fix a bug in wxPython where a MemoryView is returned in a
%ConvertFromTypeCode.
The code is this:
PyObject* i_wxPyMakeBuffer(void* ptr, Py_ssize_t len, bool readOnly=false)
{
wxPyThreadBlocker blocker;
if (ptr && len) {
Py_buffer view;
int flags = PyBUF_FORMAT|PyBUF_ND;
if (!readOnly)
flags |= PyBUF_WRITABLE;
PyBuffer_FillInfo(&view, NULL, ptr, len, readOnly ? 1:0, flags);
return PyMemoryView_FromBuffer(&view);
} else {
Py_INCREF(Py_None); return Py_None;
}
%ConvertFromTypeCode
return wxPyMakeBuffer(sipCpp->GetData(), sipCpp->GetDataLen());
%End
This "works" but the problem is that after %ConvertFromTypeCode runs, the
C++ object goes away, and since the MemoryView still references data from
the C++ object, this leads to accessing memory that has been freed. Is
there a recommended way to handle such a situation with sip? Ie, keep the
C++ object around after a %ConvertFromTypeCode? Of course, I could copy
the all the data in %ConvertFromTypeCode, but that's the thing we're
trying to avoid by using the MemoryView.
Thanks,
Scott
More information about the PyQt
mailing list