[PyQt] Object lifetime issues with QWebEngineUrlSchemeHandler and QBuffer

Phil Thompson phil at riverbankcomputing.com
Wed Sep 14 18:27:03 BST 2016


On 14 Sep 2016, at 8:49 am, Florian Bruhin <me at the-compiler.org> wrote:
> 
> Hey,
> 
> I tried implementing a custom QWebEngineUrlSchemeHandler yesterday,
> and ran into some weird object lifetime issues I can't quite explain.
> 
> The attached request1.py is the one which works fine, and displays
> "Hello World" with a custom test:foo URL.
> 
> 
> In request2.py, I removed a global reference to SchemeHandler, so
> "handler" is local in install_scheme_handler().
> 
> Now, despite setting the parent of SchemeHandler to the QApplication
> instance, it seems to go away somehow and I get an
> "ERR_UNKNOWN_URL_SCHEME" error page.
> 
> Why?

It's a bug. There is a missing /TransferThis/ annotation on the ctor.

> In request3.py, the global reference is back, but I use a QBuffer to
> which I pass a byte string instead of writing to it in requestStarted.
> Here, I get a segfault:
> 
>    #0  QBuffer::readData (this=0xe06740, data=0x7fffd4013000 "", len=32768) at io/qbuffer.cpp:412
>    #1  0x00007fffecd579ae in ?? () from /usr/lib/python3.5/site-packages/PyQt5/QtCore.so
>    #2  0x00007ffff4ae3e0a in QIODevice::read (this=0xe06740, data=0x7fffd4013000 "", maxSize=32768) at io/qiodevice.cpp:1047
>    #3  0x00007fffe734232a in QtWebEngineCore::URLRequestCustomJob::ReadRawData (this=<optimized out>, buf=0x7fff9c1692c0, bufSize=32768)
>    [...]
> 
> I explicitly keep a reference to both the QBuffer and to the
> underlying byte string, to make sure they never get collected, so why
> is this happening?

The type of the argument is actually QByteArray. The bytes object is getting converted to that under the covers and then immediately destroyed - but it needs to outlive the QBuffer. I've changed it so you'll get a type error if you don't pass a QByteArray.

> Also, with request1.py, I never delete the QBuffer - when can I do so?
> Should I do job.destroyed.connect(buf.deleteLater)?

I don't know.

Phil


More information about the PyQt mailing list