Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-71412

Creating a QPointer on the same object in multiple threads can trigger ASSERT: "!weakref.load()" in qsharedpointer_impl.h

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • Not Evaluated
    • 5.9.8, 5.11.3, 5.12.0 Beta 4
    • 5.9.7, 5.11.2, 5.12.0 Beta 3
    • Core: Other
    • None
    • Windows 10
    • Windows
    • 3b8075de3b3c842311c157476a85d2cf9ddff403 (qt/qtbase/5.11) 928e77fa14e18773f994be7cf9dcf0da52f39055 (qt/qtbase/5.9)

    Description

      I've been getting random crashes caused by a failing assertion in QWeakPointer, used internally by QPointer:

      ASSERT: "!weakref.load()" in file c:\users\qt\work\qt\qtbase\include\qtcore\../../src/corelib/tools/qsharedpointer_impl.h line 155

      I was able to isolate the crash into a simple test case, attached to this bug. The crash is very easy to trigger, usually after a couple iterations.

      The crash is caused by a bug in QtSharedPointer::ExternalRefCountData::getAndRef() that only occurs with specific timing of multiple threads trying to create the QPointer to the same object at the same time, thus the use of a thread pool in my test program.

      I have a simple fix ready to submit on gerrit already. I'm just baffled because git blame shows that this code hasn't changed in the last 8 years, so I guess this is an 8-year-old bug that somehow nobody managed to hit until I did?

      Also interestingly, the test program doesn't hit the assert on Linux. However it happens pretty easily on Windows. Maybe a difference in how quickly QThreadPool spins the worker threads on each platform?

      I've also verified that QPointer is meant to be thread-safe so my usage is correct, which is the case and in fact the bug is part of the code meant to handle the thread safety.

      Attachments

        1. main.cpp
          1 kB
        2. qpointercrashtest.pro
          0.1 kB
        For Gerrit Dashboard: QTBUG-71412
        # Subject Branch Project Status CR V

        Activity

          People

            thiago Thiago Macieira
            kromain Romain Pokrzywka
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes