0

I have created a (somewhat) simple telnet server, which creates a new thread for each connection:

void TelnetServer::incomingConnection(qintptr socketDescriptor)
{
    TelnetConnection *thread = new TelnetConnection(socketDescriptor, this);
    connect(thread, SIGNAL(shutdownRequested()), m_controller, SLOT(shutdown()));
    connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    thread->start();
}

After I disconnect the telnet client, I expected the deleteLater() slot to be called. However, findchildren of the telnet server shows that my QThread object for the (just disconnected) session still exists. It's as if the finished signal were not emitting.

As an experiment I tied the finished signal to a function that Qdebug's "FINISHED"...and it never appears. Can someone explain why the finished signal is not emitting after I disconnect the telnet client?


I am assuming that calling thread.disconnectClient() terminates the thread...but perhaps that's an incorrect assumption? To I have to quit the exec loop for the thread?

László Papp
  • 51,870
  • 39
  • 111
  • 135
TSG
  • 4,242
  • 9
  • 61
  • 121
  • finished() is emitted right after run() ends. If run() never returns, you can't expect finished() to fire. See the relevant documentation here: [http://qt-project.org/doc/qt-5.0/qtcore/qthread.html#finished](http://qt-project.org/doc/qt-5.0/qtcore/qthread.html#finished) – Alex Reinking Oct 05 '13 at 04:54

1 Answers1

1

If you disconnect a running thread, and you do not have proper treatment, the finished signal is not supposed to be called, so this is normal.

You could hook up the delete later or the disconnect "signal" that you establish, but you need to make sure to properly quit the thread.

László Papp
  • 51,870
  • 39
  • 111
  • 135
  • To properly quit a thread do I just call QThread::quit() ? If so, I'll add it to my disconnected slot for that thread. – TSG Oct 05 '13 at 05:02
  • I call tcpSocketPtr->disconnectFromHost(); to initiate a server side disconnect. The slot clientdisconnected() is then called since I tied it to the disconnected signal of the QTcpSocket. So the last line of clientdisconnected() should be QThread::quit() then? – TSG Oct 05 '13 at 05:11
  • @Michelle: have you found the answer for your question? – László Papp Nov 16 '13 at 04:43
  • Yes - I let the thread disconnect and shutdown, then delete after no long in use. – TSG Nov 17 '13 at 03:05