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. Deleting QTcpSocket cause app crash
QtWS25 Last Chance

Deleting QTcpSocket cause app crash

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtcpsocket
4 Posts 3 Posters 730 Views
  • 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.
  • D Offline
    D Offline
    Dariusz
    wrote on 10 Feb 2022, 15:50 last edited by Dariusz 2 Oct 2022, 15:50
    #1

    Hey

    This is an interesting one. I have a worker thread.
    Worker thread has a MainA-QObject that has some children, one of which is QTcpSocket. All of them live inside the worker thread.
    The Child gets added to MainA during creation as socket(MainA) or via setParent(MainA)

    When MainA object dies, all its children get to be deleted too... but here qt crashes on me somehow. It's not always, it's like 5% chance it crash and is very random. My MainA is a QSharedPointer<>, Here is the picture of log >

    e7e7e403-4601-41d7-b3c9-464325eebe49-image.png
    QList.cpp
    d5d638b2-60b9-491d-bd32-bdcba2f3e4d6-image.png

    QObject>
    5a841e47-6db5-4fd6-86f8-e42b0f561a74-image.png

    Error Exception: Exception 0xc0000005 encountered at address 0x7ffd84b3bd27: Access violation reading location 0xffffffffffffffff

    It looks to me like extraData can be invalid & cause the core crash?
    Given my complex thread logic... it may be a bit difficult to reproduce it.
    I do quite few of >

                if (mSocketPtr) {
                    QMetaObject::invokeMethod(this, [this]() {
                        QMetaObject::invokeMethod(mSocketPtr, [this]() {
                            mSocketPtr->setParent(this);
                        }, Qt::QueuedConnection);
                    }, Qt::QueuedConnection);
                }
    

    When I'm creating/moving objects. Feels like this can happen when objects gets deleted before properly getting moved to correct parent or something?

    f64ec8a5-2f42-429d-874f-083c6431e919-image.png

    C K 2 Replies Last reply 10 Feb 2022, 17:49
    0
    • D Dariusz
      10 Feb 2022, 15:50

      Hey

      This is an interesting one. I have a worker thread.
      Worker thread has a MainA-QObject that has some children, one of which is QTcpSocket. All of them live inside the worker thread.
      The Child gets added to MainA during creation as socket(MainA) or via setParent(MainA)

      When MainA object dies, all its children get to be deleted too... but here qt crashes on me somehow. It's not always, it's like 5% chance it crash and is very random. My MainA is a QSharedPointer<>, Here is the picture of log >

      e7e7e403-4601-41d7-b3c9-464325eebe49-image.png
      QList.cpp
      d5d638b2-60b9-491d-bd32-bdcba2f3e4d6-image.png

      QObject>
      5a841e47-6db5-4fd6-86f8-e42b0f561a74-image.png

      Error Exception: Exception 0xc0000005 encountered at address 0x7ffd84b3bd27: Access violation reading location 0xffffffffffffffff

      It looks to me like extraData can be invalid & cause the core crash?
      Given my complex thread logic... it may be a bit difficult to reproduce it.
      I do quite few of >

                  if (mSocketPtr) {
                      QMetaObject::invokeMethod(this, [this]() {
                          QMetaObject::invokeMethod(mSocketPtr, [this]() {
                              mSocketPtr->setParent(this);
                          }, Qt::QueuedConnection);
                      }, Qt::QueuedConnection);
                  }
      

      When I'm creating/moving objects. Feels like this can happen when objects gets deleted before properly getting moved to correct parent or something?

      f64ec8a5-2f42-429d-874f-083c6431e919-image.png

      C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 10 Feb 2022, 17:49 last edited by
      #2

      @Dariusz said in Deleting QTcpSocket cause app crash:

              if (mSocketPtr) {
                  QMetaObject::invokeMethod(this, [this]() {
                      QMetaObject::invokeMethod(mSocketPtr, [this]() {
                          mSocketPtr->setParent(this);
                      }, Qt::QueuedConnection);
                  }, Qt::QueuedConnection);
              }
      

      WTF?

      Please provide a minimal, compilable example.

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

      D 1 Reply Last reply 11 Feb 2022, 09:41
      3
      • C Christian Ehrlicher
        10 Feb 2022, 17:49

        @Dariusz said in Deleting QTcpSocket cause app crash:

                if (mSocketPtr) {
                    QMetaObject::invokeMethod(this, [this]() {
                        QMetaObject::invokeMethod(mSocketPtr, [this]() {
                            mSocketPtr->setParent(this);
                        }, Qt::QueuedConnection);
                    }, Qt::QueuedConnection);
                }
        

        WTF?

        Please provide a minimal, compilable example.

        D Offline
        D Offline
        Dariusz
        wrote on 11 Feb 2022, 09:41 last edited by
        #3

        @Christian-Ehrlicher Yeah that part is a bit dramatic... but a bit needed in order for all to get made in the proper thread if its being made in some odd thread. Since parent object can be made in threadD but then assigned thread C for processing and in the same time socket can be made in thread X and then he has to be moved to correct thread C but that may be delayed due to queue etc :D It's banana I know. This is so that objects gets handled/made asap and then pushed to correct threads on next loop.

        In any case, I figured it out! I think...
        Essentially
        From threadX a socket could die, and then notification gets emitted, the problem is that this may be handled by a different thread and since I had QSharedPointer handling the lifetime of an object... it could be deleted from any thread, some times not socket thread! So I Had to implement custom delete on my sharedPointer and call this->deleteLater() from that rather than having it delete this;... Oh that was fun :- )

        Pain to find this error with all the tread switching/etc. Crashed like 1% of the time sigh.

        Seems to run now smoothly.

        So for any1 interested, to delete qt object using shared pointer , reimplement destructor call on shared pointer and do deleteLater() in it.

        1 Reply Last reply
        0
        • D Dariusz
          10 Feb 2022, 15:50

          Hey

          This is an interesting one. I have a worker thread.
          Worker thread has a MainA-QObject that has some children, one of which is QTcpSocket. All of them live inside the worker thread.
          The Child gets added to MainA during creation as socket(MainA) or via setParent(MainA)

          When MainA object dies, all its children get to be deleted too... but here qt crashes on me somehow. It's not always, it's like 5% chance it crash and is very random. My MainA is a QSharedPointer<>, Here is the picture of log >

          e7e7e403-4601-41d7-b3c9-464325eebe49-image.png
          QList.cpp
          d5d638b2-60b9-491d-bd32-bdcba2f3e4d6-image.png

          QObject>
          5a841e47-6db5-4fd6-86f8-e42b0f561a74-image.png

          Error Exception: Exception 0xc0000005 encountered at address 0x7ffd84b3bd27: Access violation reading location 0xffffffffffffffff

          It looks to me like extraData can be invalid & cause the core crash?
          Given my complex thread logic... it may be a bit difficult to reproduce it.
          I do quite few of >

                      if (mSocketPtr) {
                          QMetaObject::invokeMethod(this, [this]() {
                              QMetaObject::invokeMethod(mSocketPtr, [this]() {
                                  mSocketPtr->setParent(this);
                              }, Qt::QueuedConnection);
                          }, Qt::QueuedConnection);
                      }
          

          When I'm creating/moving objects. Feels like this can happen when objects gets deleted before properly getting moved to correct parent or something?

          f64ec8a5-2f42-429d-874f-083c6431e919-image.png

          K Offline
          K Offline
          KroMignon
          wrote on 11 Feb 2022, 10:46 last edited by KroMignon 2 Nov 2022, 10:55
          #4

          @Dariusz said in Deleting QTcpSocket cause app crash:

          My MainA is a QSharedPointer<>, Here is the picture of log >

          I hope you have create it like this QSharedPointer<MainA>(new MainA(), &QObject::deleteLater);

          It looks to me like extraData can be invalid & cause the core crash?
          Given my complex thread logic... it may be a bit difficult to reproduce it.
          I do quite few of

                  if (mSocketPtr) {
                      QMetaObject::invokeMethod(this, [this]() {
                          QMetaObject::invokeMethod(mSocketPtr, [this]() {
                              mSocketPtr->setParent(this);
                          }, Qt::QueuedConnection);
                      }, Qt::QueuedConnection);
                  }
          

          I do not understand this!
          If you want to change parent for a class instance, why do it so complicat?
          Setting parent can only be done if children and parent in same thread, but it muss not be done in the work thread!
          I would do it like this:

          if(mSocketPtr && mSocketPtr->parent() != this)
          {
              if(mSocketPtr->thread() != this->thread())
              {
                  QMetaObject::invokeMethod(mSocketPtr, [this]() {
                              mSocketPtr->moveToThread(this->thread());
                              mSocketPtr->setParent(this);
                          });
              }
              else
                 mSocketPtr->setParent(this);
          }
          

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          1

          1/4

          10 Feb 2022, 15:50

          • Login

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