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 QSslSocket obj causes crash.
Qt 6.11 is out! See what's new in the release blog

Deleting QSslSocket obj causes crash.

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.6k Views 1 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.
  • C Offline
    C Offline
    chooser
    wrote on last edited by
    #1

    Hi was wondering if anybody understands this problem I am seeing.

    I have a simple SSL client and server. Connection is fine. Communication is fine. When the client disconnects the server needs to delete the QSslSocket object, I would imagine. However when I do that the server seg faults. I don't understand what's going on. I expected this to be really straightforward but apparently i missing some QT (or other) concept.

    Can anybody help out? Thanks

    Essential server code:
    @
    void SSLServer::incomingConnection(int sd)
    {
    sslSocket = new SSLSocket(this);
    if( sslSocket->setSocketDescriptor(sd))
    {
    QFile sslkeyfile(privKey_);
    sslSocket->setPrivateKey(QSslKey(sslkeyfile.readAll(),QSsl::Rsa));

        QFile cliCertFile(serverCert_);
        sslSocket->setLocalCertificate(QSslCertificate(cliCertFile.readAll()));
    
        QFile certFile(caCert_);
        sslSocket->addCaCertificate(QSslCertificate(certFile.readAll()));
    
        sslSocket->setPeerVerifyMode(QSslSocket::VerifyPeer);
        sslSocket->setPeerVerifyDepth(1);        
        sslSocket->setProtocol(QSsl::SslV3);
    
        connect(sslSocket, SIGNAL(error(QAbstractSocket::SocketError)),
                       this, SLOT(error_handler(QAbstractSocket::SocketError)));
        connect(sslSocket, SIGNAL(sslErrors(QList<QSslError>)),
                       this, SLOT(ssl_error_handler(QList<QSslError>)));
        connect(sslSocket, SIGNAL(encrypted()), this, 
                      SLOT(ready()));
        connect(sslSocket, SIGNAL(readyRead()), this, 
                      SLOT(read_data_from_client()));
    
        sslSocket->startServerEncryption();
        if(!sslSocket->waitForEncrypted())
        {
            qDebug() <<  "failed to perform SSL handshake with client";
            QList<QSslCertificate> caChain = sslSocket->caCertificates();
            return;
        }
    }
    

    }

    void SSLServer::read_data_from_client()
    {
    QByteArray qstrbytes = sslSocket->readAll();
    qDebug() << Q_FUNC_INFO << qstrbytes;
    }

    void SSLServer::ready()
    {
    QSslCertificate clientCert = sslSocket->peerCertificate();
    qDebug() << clientCert.isValid();
    }

    void SSLServer::error_handler(QAbstractSocket::SocketError in_error)
    {
    qDebug() << Q_FUNC_INFO << in_error;
    if(in_error == QAbstractSocket::RemoteHostClosedError)
    {
    delete sslSocket; //// line causes crash !!!!!!!!!!
    }
    }
    @

    The seg fault happens here in QT's own QAbstractSocketPrivate::resetSocketLayer().
    socketEngine is invalid, garbage pointer, But what am I supposed to do about it.

    @void QAbstractSocketPrivate::resetSocketLayer()
    {
    #if defined (QABSTRACTSOCKET_DEBUG)
    qDebug("QAbstractSocketPrivate::resetSocketLayer()");
    #endif

    if (socketEngine) {
        socketEngine->close(); //// right here crash happens!!!
        socketEngine->disconnect();
        delete socketEngine;
        socketEngine = 0;
        cachedSocketDescriptor = -1;
    }
    if (connectTimer)
        connectTimer->stop();
    if (disconnectTimer)
        disconnectTimer->stop();
    

    }@

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

      Hi,

      Could you test replacing delete with

      @sslSocket->deleteLater();@

      ?

      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