Unsolved Global static QPixmapCache in Qt internals
-
There are some reports about such an issue:
https://bugreports.qt.io/browse/QTBUG-48709
https://bugreports.qt.io/browse/QTBUG-21807And it does not crash here for me on Linux and Windows (MSVC, debug-build) with 5.15.x
-
@Christian-Ehrlicher said in Global static QPixmapCache in Qt internals:
And it does not crash here for me on Linux and Windows (MSVC, debug-build) with 5.15.x
We are talking about the QTimers from another thread message. JKSH, split the threads. The other stuff (the supposed crash) contines to befuddle me.
-
I also don't have a warning about the timer with the testcase on windows/msvc :)
-
We are talking about the QTimers from another thread message. JKSH, split the threads. The other stuff (the supposed crash) contines to befuddle me.
I don't know how exactly the crash in the original thread is connected with this. However, the crash and the message about timers are appearing together. They appear after any interaction with simple
QPushButton
. UsingdeleteLater()
as suggested by @JKSH solves the crash, but the message persists.@Christian-Ehrlicher said in Global static QPixmapCache in Qt internals:
And it does not crash here for me on Linux and Windows (MSVC, debug-build) with 5.15.x
To clarify: crash appears only when one tries to unload a library with
QApplication
inside. The library in my case is loaded bylua.exe
, but I doubt it is lua-related problem. -
@kshegunov said in Global static QPixmapCache in Qt internals:
Possibly. Or it can be a pseudo-singleton (similarly to
QCoreApplication
) and be initialized on demand in the instance retrieving function (dropping the global static that it currently uses). You could poke Thiago, but as written, I wouldn't hold my breath.That sounds like a source and binary compatibility break, unless instance() is used from within the existing static functions.
A thread_local version would be another solution, that could be implemented without changing any user source code. Keep the global static interface object for binary compatibility, and move the actual QObject-based implementation to thread_local storage for the gui thread.
The QPixmapCache documentation already says that the cache is only usable from the "application's main thread".
-
@jeremy_k said in Global static QPixmapCache in Qt internals:
That sounds like a source and binary compatibility break, unless instance() is used from within the existing static functions.
Adding non-virtual functions doesn't brake API or ABI compatibility. Also this is an internal member, any global getter function could do, it's not really necessary to expose it to the user.
A thread_local version would be another solution, that could be implemented without changing any user source code. Keep the global static interface object for binary compatibility, and move the actual QObject-based implementation to thread_local storage for the gui thread.
The QPixmapCache documentation already says that the cache is only usable from the "application's main thread".
It's an internal class, I didn't intend to modify the
QPixmapCache
to begin with. (see the woboq link in Chris' post) -
@kshegunov said in Global static QPixmapCache in Qt internals:
@jeremy_k said in Global static QPixmapCache in Qt internals:
That sounds like a source and binary compatibility break, unless instance() is used from within the existing static functions.
Adding non-virtual functions doesn't brake API or ABI compatibility. Also this is an internal member, any global getter function could do, it's not really necessary to expose it to the user.
I had read it as a suggestion to introduce a static QPixmapCache::instance(), and make the currently static QPixmapCache member functions non-static. I autocompleted too much.
A thread_local version would be another solution, that could be implemented without changing any user source code. Keep the global static interface object for binary compatibility, and move the actual QObject-based implementation to thread_local storage for the gui thread.
The QPixmapCache documentation already says that the cache is only usable from the "application's main thread".
It's an internal class, I didn't intend to modify the
QPixmapCache
to begin with. (see the woboq link in Chris' post)I saw, and if I interpreted correctly, the issue is that the global static QPixmapCache and its internal QObject based implementation will be destroyed from the C++ main thread when main() exits. The destruction of the internal implementation object could be removed from the public QPixmapCache destructor, but the question is where to destroy it while maintaining the current intended behavior. Making the internal object (or a wrapper) thread local in the gui thread solves destruction at a point where the cache can't see further use anyway.
-
@jeremy_k said in Global static QPixmapCache in Qt internals:
I saw, and if I interpreted correctly, the issue is that the global static QPixmapCache and its internal QObject based implementation will be destroyed from the C++ main thread when main() exits.
Correct, but only with the allowance that we are talking about the
QPMCache
instance (which is some internal class).The destruction of the internal implementation object could be removed from the public QPixmapCache destructor, but the question is where to destroy it while maintaining the current intended behavior.
As I wrote:
QCoreApplication::aboutToQuit
is one possible such point. The problem with that solution is that it won't set the nullptr of the global pointer, which in turn is going to lead to a crash if the application object is recreated. It shouldn't get recreated, but I've seen code that relies on that (believe or not). With any API the first thing you get is hidden use cases, sadly.Making the internal object (or a wrapper) thread local in the gui thread solves destruction at a point where the cache can't see further use anyway.
I don't follow. The instance is created dynamically and on demand, and from some thread unknown at the point of declaration. How does specifying a thread local storage helps here, the pointer to the object is global (and I assume used from many a place)?
-
@kshegunov said in Global static QPixmapCache in Qt internals:
@jeremy_k said in Global static QPixmapCache in Qt internals:
I saw, and if I interpreted correctly, the issue is that the global static QPixmapCache and its internal QObject based implementation will be destroyed from the C++ main thread when main() exits.
Correct, but only with the allowance that we are talking about the
QPMCache
instance (which is some internal class).Yes. We seem to be talking around the same point.
The destruction of the internal implementation object could be removed from the public QPixmapCache destructor, but the question is where to destroy it while maintaining the current intended behavior.
As I wrote:
QCoreApplication::aboutToQuit
is one possible such point. The problem with that solution is that it won't set the nullptr of the global pointer, which in turn is going to lead to a crash if the application object is recreated. It shouldn't get recreated, but I've seen code that relies on that (believe or not). With any API the first thing you get is hidden use cases, sadly.Making the internal object (or a wrapper) thread local in the gui thread solves destruction at a point where the cache can't see further use anyway.
I don't follow. The instance is created dynamically and on demand, and from some thread unknown at the point of declaration. How does specifying a thread local storage helps here, the pointer to the object is global (and I assume used from many a place)?
Using QCoreApplication::aboutToQuit would change the behavior for applications that repeatedly instantiate and destroy the application instance, and it would add an opportunity to make a mistake for applications that don't use QCoreApplication::exec().
Using a thread local object keeps this into RAII territory. The only difference from the current global static is that destruction happens in the relevant thread rather than in the main() thread.
How does the cache managing object tell that it is in the gui thread? If the first application instance must be created before cache use, QThread::currentThread()->threadId() == QCoreApplication::instance()->thread()->threadId() could work. I don't know if creating the application first is currently required.
-
@jeremy_k said in Global static QPixmapCache in Qt internals:
Using a thread local object keeps this into RAII territory. The only difference from the current global static is that destruction happens in the relevant thread rather than in the main() thread.
Ehm, the object, currently, is created in the heap, is it not? Or perhaps I'm misunderstanding what you mean exactly.
@jeremy_k said in Global static QPixmapCache in Qt internals:
I don't know if creating the application first is currently required.
Yes, it is, but also no
QObject
shall outlive theQCoreApplication
instance is part of the rule, which is exactly what's happening in this case.PS.
How does the cache managing object tell that it is in the gui thread?
When a
QObject
is created it stores the reference to the creating thread internally. When the destructor's run that reference's thread id is checked against the current thread id, if they don't match you know you're destroying objects from another thread. This is where this warning is generated. -
@kshegunov said in Global static QPixmapCache in Qt internals:
@jeremy_k said in Global static QPixmapCache in Qt internals:
Using a thread local object keeps this into RAII territory. The only difference from the current global static is that destruction happens in the relevant thread rather than in the main() thread.
Ehm, the object, currently, is created in the heap, is it not? Or perhaps I'm misunderstanding what you mean exactly.
IIRC, it is created and destroyed by a QGlobalStatic.
@jeremy_k said in Global static QPixmapCache in Qt internals:
I don't know if creating the application first is currently required.
Yes, it is, but also no
QObject
shall outlive theQCoreApplication
instance is part of the rule, which is exactly what's happening in this case.The cache object also outlives a stack allocated QCoreApplication when the gui thread is the main thread.
I'm looking for and not finding a reference to the rules about QCoreApplication versus QObject lifetimes. There used to be a concise list, if I didn't imagine it.
-
@jeremy_k said in Global static QPixmapCache in Qt internals:
IIRC, it is created and destroyed by a QGlobalStatic.
Yes, correct. So you want to make the
QGlobalStatic
instance thread local then?@jeremy_k said in Global static QPixmapCache in Qt internals:
The cache object also outlives a stack allocated QCoreApplication when the gui thread is the main thread.
Yes, but since they're created and destroyed in the same thread the message doesn't fire up. It's wrong, it's just hidden.
@jeremy_k said in Global static QPixmapCache in Qt internals:
I'm looking for and not finding a reference to the rules about QCoreApplication versus QObject lifetimes. There used to be a concise list, if I didn't imagine it.
I don't remember such a list, but this is a direct quote of Thiago[1]:
And QCoreApplication should be the first QObject created and last destroyed.
-
@kshegunov said in Global static QPixmapCache in Qt internals:
@jeremy_k said in Global static QPixmapCache in Qt internals:
IIRC, it is created and destroyed by a QGlobalStatic.
Yes, correct. So you want to make the
QGlobalStatic
instance thread local then?That would be nice.
QTheadLocalStatic
. I don't see a lot of thread_local in Qt 6 base.@jeremy_k said in Global static QPixmapCache in Qt internals:
I'm looking for and not finding a reference to the rules about QCoreApplication versus QObject lifetimes. There used to be a concise list, if I didn't imagine it.
I don't remember such a list, but this is a direct quote of Thiago[1]:
And QCoreApplication should be the first QObject created and last destroyed.
It's not the reference I was thinking of, but thanks.
-
@jeremy_k said in Global static QPixmapCache in Qt internals:
That would be nice. QTheadLocalStatic. I don't see a lot of thread_local in Qt 6 base.
Because it has very niche uses to begin with. Making the mentioned object thread local implies an instance being created for each thread that runs. It could work, but I'm not convinced it's necessary to create the wrapper object for each thread Qt or the user starts. Granted it's just some pointer wrapper, but still ... there should be a cleaner way, I think.
PS
We are overengineering the solution. The pixmap cache is only supposed to work from one thread and the GUI one at that. You just revert to a plain pointer and define an inline function that creates the object if not there. (It also connects a lambda for deletion and clearing the pointer itself). The uses seem to be limited only to theQPixmapCache
implementation itself.PS 2
Alternatively you convert it toQPointer
and create the object in theQPixmapCache
constructor where you also connect theQCoreApplication::aboutToQuit
to theQObject::deleteLater
for the internal object. -
@kshegunov said in Global static QPixmapCache in Qt internals:
PS 2
Alternatively you convert it toQPointer
and create the object in theQPixmapCache
constructor where you also connect theQCoreApplication::aboutToQuit
to theQObject::deleteLater
for the internal object.This idea is sound, with a small adjustment to avoid initializing the object if it is never used (same as QGlobalStatic): https://lists.qt-project.org/pipermail/development/2021-May/041517.html
-
I saw the mail. I think there was some reason I decided against it, but can't remember currently. For sure one of it is that there's no
QPixmapCache
constructor ...Anyway, the
QPointer
is superfluous, as you're always the owner of the object. It never can get destroyed midway and you don't need to listen for it.