Priority: P1: Critical
Affects Version/s: 4.7.3
Fix Version/s: 4.8.0
Component/s: Core: Event System
Microsoft Windows. The sample application works with MinGW and VC++ compilers, run in QtCreator 2.2.
Signals in our server application were being received in the application in the wrong order.
The problem was traced to the to the signals actually being sent in the wrong order from Qt library (QCoreApplicationPrivate::sendPostedEvents).
Further inspection of the code showed that the reason that the signals were being sent out of order was because the events that represent those signals were being added out of order to the list of events to post (QCoreApplication::postEvent).
When inserting a new event to the post-event-list, the code was using a binary search method (qUpperBound) to determine where the event should be placed(the list is sorted by event priority), but the list happened to be unsorted and thus the binary search gave a bogus answer (and the event inserted in the wrong position) since being sorted is a prerequisite for binary search to succeed.
Since the code in QCoreApplication::postEvent itself was correctly taking event sorting into account we started looking for other places that modified the list to make sure the sorting was also preserved.
We found QObjectPrivate::setThreadData_helper the method responsible of moving events from one event list to another when a given QObject is moved from a thread to a different one. This method was appending events to the target list without taking into account the sorting by priority, resulting in a unsorted list.
We decided to modify QObjectPrivate::setThreadData_helper to take into account the priority when inserting events to the list (by using the same code QCoreApplication::postEvent uses). After this change both the original tournament model code and the simple tester class did not report signals being received in the wrong order.
There is still one place in (QCoreApplicationPrivate::sendPostedEvents) where events are being added to the event list without taking priority into account, it is a edge case that we have not been able to reproduce. We are still working on creating a reproduceable case within the confines of a test application.