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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top