Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. ASSERT: "isDetached()" with multithreaded QVectors
Forum Updated to NodeBB v4.3 + New Features

ASSERT: "isDetached()" with multithreaded QVectors

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 7 Posters 5.9k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J jars121

    I appear to have fixed the issue with the following changes:

    1. Removed the duplication error mentioned above; I was accidentally instantiating my main class twice.
    2. I created a new class to manage the shared QVectors separate to my main class.
    3. I've incorporated QMutexLockers into each function which modifies the shared QVectors.

    The application has been running for around 20 minutes now without issue, so I'm hopeful that the detaching error is resolved.

    aha_1980A Offline
    aha_1980A Offline
    aha_1980
    Lifetime Qt Champion
    wrote on last edited by
    #8

    @jars121 thanks for your feedback.

    sounds like a clean solution.

    so please mark this topic as SOLVED now. Thanks

    Qt has to stay free or it will die.

    1 Reply Last reply
    1
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #9

      Hi,

      Just an additional tool worth mentioning: QReadWriteLock. If you are reading from several threads then you can easily access your data and as soon as there's a write going it will lock the access. It might be faster than QMutex.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • J Offline
        J Offline
        jars121
        wrote on last edited by
        #10

        Thanks @SGaist I had seen mention of QReadWriteLock in my travels, but hadn't yet put any serious thought into it. I'll be sure to read further and do some testing between it and QMutex.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jars121
          wrote on last edited by
          #11

          I've marked this as 'Unsolved', as I'm afraid I'm still battling this "isDetached()" assertion.

          Following SGaist's recommendation above, I've actually gone through my entire application and removed all QMutex, and replaced them with QReadWriteLocks. In short, wherever I'm reading from or writing to a shared (between QThreads) QVector, QList, etc., there's a QReadLocker or QWriteLocker (respectively) present. However, the issue remains that after a seemingly random period of time, the application will crash (in Debug mode) with the "isDetached()" assertion.

          I've done some further research on this issue, and found a similar reference to this assertion here. The findings/recommendations in that thread boil down to this:

          • Using the [] operator to retrieve an item at the specified index in a QVector can create a deep copy situation, which is from where a detached() scenario could arise. This is specified in the QVector Class documentation.
          • Using at() to retrieve an item at the specified index in a QVector is the preferred approach if not modifying the QVector.

          As it turns out, I've never used at() before, and have been completely unaware of this distinction. I'm going through my code now, with the intention of replacing all read-only QVector operator references with at().

          The use of at() won't assert "isDetached()", so hopefully this solves this particular issue once and for all. I'll update the thread with my findings.

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jars121
            wrote on last edited by
            #12

            Well the above certainly doesn't appear to have made any difference whatsoever. I've replaced every single instance of read-only [] QVector and QList operator with .at(), but the "isDetached()" assertion is still present.

            One question as I continue to review the c++ side of my application; when reading from an exposed c++ QVector in QML, is vector[i] the only/recommended way of retrieving a value from index i? The reason I ask is I might accidentally be inducing a deep copy on the QML side without knowing it. The documentation suggests that the [] operators are the correct approach, but I wanted to confirm before dismissing QML as the potential cause of this extremely frustrating issue.

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #13

              This will definitely not fix your problems but only hide it. When using objects in multiple threads you have to sync the access with a mutex or similar as explained by SGaist and others. If you don't do it you will get into trouble sooner or later.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              2
              • J Offline
                J Offline
                jars121
                wrote on last edited by
                #14

                I figured that's what I was doing by using QReadWriteLock and corresponding QReadLockers and QWriteLockers when interacting with the shared QVectors, QLists, etc?

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  jars121
                  wrote on last edited by
                  #15

                  Ok so I've removed all the QReadWriteLock, QReadLocker and QWriteLocker instances, and have gone back to QMutex with QMutexLockers. I've been somewhat more diligent this time around, and in combination with the change from [] operators to .at() the application seems to be running smoothly.

                  I've got further testing to do before I'm completely comfortable, so I'll leave the thread open for the time being.

                  Y 1 Reply Last reply
                  0
                  • J jars121

                    Ok so I've removed all the QReadWriteLock, QReadLocker and QWriteLocker instances, and have gone back to QMutex with QMutexLockers. I've been somewhat more diligent this time around, and in combination with the change from [] operators to .at() the application seems to be running smoothly.

                    I've got further testing to do before I'm completely comfortable, so I'll leave the thread open for the time being.

                    Y Offline
                    Y Offline
                    Yippiyak
                    wrote on last edited by
                    #16

                    @jars121

                    did you ever solve this issue? I have the same problem

                    1 Reply Last reply
                    1
                    • Z Offline
                      Z Offline
                      Zhengyang
                      wrote on last edited by
                      #17

                      Hi, @jars121
                      Have you found a solution? I have the same problem. And I can not solve it by adding a QMutex.

                      SGaistS 1 Reply Last reply
                      0
                      • Z Zhengyang

                        Hi, @jars121
                        Have you found a solution? I have the same problem. And I can not solve it by adding a QMutex.

                        SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #18

                        @Zhengyang hi and welcome to devnet, it's described in the last post of @jars121.

                        If QMutex does not solve your issue, you are likely misusing it.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved