[Interest] QTSPCosket signal disconnected()

Igor Mironchik imironchick at gmail.com
Mon Aug 20 20:50:26 CEST 2012


Hello.

In the attachement you can find test applications. All applications 
(server and client) are one-thread apps.

But something going stange in the server. I specially added QMutex to 
ServerSocket and when
client disconnected from server application catches deadlock. How it's 
possible? May be it's a bug in Qt?

It's simple to reproduce: just run server then client and then stop 
client...

Look at the code:

//
// ServerSocketPrivate
//

struct ServerSocket::ServerSocketPrivate {
     QMutex m_mutex;
     QList< ClientSocket* > m_clients;
};


//
// ServerSocket
//

ServerSocket::ServerSocket( QObject * parent )
     :    QTcpServer( parent )
     ,    d( new ServerSocketPrivate )
{
}

ServerSocket::~ServerSocket()
{
}

void
ServerSocket::incomingConnection( int socketDescriptor )
{
     ClientSocket * socket = new ClientSocket( this );

     if( socket->setSocketDescriptor( socketDescriptor ) )
     {
         connect( socket, SIGNAL( disconnected() ),
             this, SLOT( slotDisconnected() ) );

         {
             MutexLocker lock( &d->m_mutex );

             d->m_clients.append( socket );
         }
     }
     else
         delete socket;
}

void
ServerSocket::slotDisconnected()
{
     qDebug() << "slotDisconnected()";

     MutexLocker lock( &d->m_mutex );

     sender()->deleteLater();
}

void
ServerSocket::doThis()
{
     qDebug() << "doThis()";

     MutexLocker lock( &d->m_mutex );

     foreach( ClientSocket * socket, d->m_clients )
         socket->sendData();
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.zip
Type: application/octet-stream
Size: 7215 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120820/0e01cd90/attachment.obj>


More information about the Interest mailing list