[Qt-interest] [Solved] Using QSslSocket with QTcpServer results in segfault

Nikos Chantziaras realnc at arcor.de
Sun Dec 6 10:26:30 CET 2009


On 12/06/2009 10:31 AM, Nikos Chantziaras wrote:
> On 12/06/2009 10:04 AM, Thiago Macieira wrote:
>> Em Domingo 6. Dezembro 2009, às 08.50.54, Nikos Chantziaras escreveu:
>>> The rest of the "Fortune Server" app stays the same.  However, when
>>> connecting to the server, the application crashes instantly with:
>>>
>>>      QObject::connect: Cannot connect (null)::disconnected() to
>>>      (null)::deleteLater()
>>>      Segmentation fault
>>>
>>> What am I doing wrong?
>>
>>  [...]
>> There, you're connecting a null pointer's signal to its slot. What happened
>> next is anyone's guess (since you didn't paste it): mine is that you called
>> something on that null pointer, which resulted in the crash.
>
> You are right.  It seems I made the wrong assumption that QSslSocket can
> be used as a drop-in replacement for QTcpSocket like this:
>
>     QTcpSocket* socket = fTcpServer->nextPendingConnection();
>     connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater()));
>
> fTcpServer actually points to an SslServer object and in this case
> nextPendingConnection() returns 0.  I was trying to use polymorphism
> here in order to leave that code alone.  But I guess it's never that easy :P

For everyone looking this up in search for a solution:

Overriding QTcpServer::incomingConnection() is not enough.  You also 
need to override QTcpServer::nextPendingConnection() and return the 
QSslSocket you created in incomingConnection().  You can store the 
QSslSockets created there in QQueue and return them in 
nextPendingConnection().  For example:
(Not very correct, it's just to get your "hello world" SSL experiments 
going.)

void SSLServer::incomingConnection( int socketDescriptor )
{
   QSslSocket* socket = new QSslSocket(this);
   // ... set up the socket according to your needs.

   if (socket->setSocketDescriptor(socketDescriptor)) {
     // fConnections is a QQueue<QSslSocket*>
     this->fConnections.enqueue(socket);
     // ... the rest stays as it was.
   } else {
     delete socket;
   }
}

QTcpSocket* SSLServer::nextPendingConnection()
{
   // Return the next connection in the queue. If there is none,
   // return 0.
   if (this->fConnections.isEmpty()) {
     return 0;
   } else {
     return this->fConnections.dequeue();
   }
}




More information about the Qt-interest-old mailing list