[Development] Should null QPixmaps be allowed in a QCoreApplication?

Sze Howe Koh szehowe.koh at gmail.com
Fri Sep 3 14:38:31 CEST 2021


On Tue, 31 Aug 2021 at 19:22, Eirik Aavitsland <eirik.aavitsland at qt.io> wrote:
>
> On 7/27/21 6:41 PM, Sze Howe Koh wrote:
> > Current Qt behaviours:
> >
> > A) If you create any QPixmap after creating QGuiApplication, the result
> > is probably the pixmap that you asked for. All is well.
> > B) If you create any QPixmap after creating QCoreApplication, the result
> > is a null QPixmap. No warnings are produced.
> > C) If you create any QPixmap _in a secondary thread_ after creating
> > QCoreApplication, the result is a segfault due to a nullptr dereference [1].
> > D) If you create any QPixmap without creating Q(Core|Gui|)Application,
> > the result is a qFatal() telling you that you must have a QGuiApplication.
> >
>
> The description of B) is not quite correct I think. The actual behaviour
> is that, if you have a QCoreApplication, then
> B1) If you create a QPixmap through QPixmap::fromImage(), either
> directly or indirectly via QVariant or reading from QDataStream, you get
> a runtime warning and a null QPixmap.
> B2) If you create a QPixmap any other way, you get a qFatal() with a
> message, i.e. same as D).

Ah, OK. I only constructed null pixmaps in my test (calling the
QPixmap constructor with no arguments) and there was no runtime
warning.

So really, it's
B1) If you create a QPixmap through QPixmap::fromImage()... you get a
runtime warning and a null QPixmap.
B2) If you create a _non-null_ QPixmap any other way, you get a
qFatal() with a message
B3) If you create a null QPixmap, you get no warnings and have nothing
to worry about.

> Now the logs indicate that the exception for the B1 situation is there
> to let headless, QCoreApplication-based apps handle QDataStreams that
> may contain QPixmaps without crashing. DBus is mentioned as a case. This
> is the reason there is an autotest that explicitly ensures this
> behaviour. It was clearly done intentionally.
>
> What is anyway clear is that behaviour C) is not intended and is a bug.
> So C) can and should be fixed; to behave the same as B), I think.
>
> - Eirik Aa.

If it's as designed, then I'm happy to leave it (and happy to avoid
breaking any user code!)

So to clarify: The answer to my original question is "Yes, null
QPixmaps are allowed in a QCoreApplication"? In that case, isn't the
qFatal() unnecessary in qt_pixmap_thread_test()?


Regards,
Sze-Howe


More information about the Development mailing list