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

Crash on QAbstractSocket in deleted QSharedPointer

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 6.2.4
    • Network
    • None
    • Windows

    Description

      I have written an application managing communications between two computers, using a single Ethernet wire, but two sockets (corresponding to two different ports). When an error occurs on a socket (whatever the type of the error), I'm disconnecting and deleting the two sockets. So, my code is something like this:

      class DoubleSocket : public QObject
      {
      Q_OBJECT
      private:
      QSharedPointer<QTcpSocket> m_socket1;
      QSharedPointer<QTcpSocket> m_socket2;
       
      DoubleSocket(QString p_ip)
      : m_socket1(new QTcpSocket)
      , m_socket2(new QTcpSocket)
      {
        QObject::connect(m_socket1.data(), QAbstractSocket::errorOccured, this, DoubleSocket::disconnectAll);
        QObject::connect(m_socket2.data(), QAbstractSocket::errorOccured, this, DoubleSocket::disconnectAll);
        m_socket1->connectToHost(m_ip, 1024);
        m_socket1->connectToHost(m_ip, 1025);
      }
       
      void disconnectAll()
      {
        if (!m_socket1.isNull())
        {
          m_socket1->disconnectFromHost();
          m_socket1->waitForDisconnected();
          m_socket1.reset();
        }
       
        if (!m_socket2.isNull())
        {
          m_socket2->disconnectFromHost();
          m_socket2->waitForDisconnected();
          m_socket2.reset();
        }
      }
      };
      

       
      To test it, I'm disconnecting the Ethernet wire. Then a crash occurs because: * The error detected on socket1 deletes the two sockets,

      • The error on socket2 is also present. As it is a disconnect error, Qt internally executes the disconnectFromHost. But everything related to the second socket has already been deleted. This is this situation that creates a crash.

      On my side I solved the problem by replacing the call of reset() by a call to deleteLater().
       

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            manordheim Mårten Nordheim
            alakauf Alain Kaufmann
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes