Windows 7 の Qt 4.7 で RPC サーバーを開発しています。同時に複数の実行に参加するために、すべての要求が個別のスレッドで実行されます (関数がブロックされている可能性があるため)。QTcpServer から継承し、incomingConnection 関数を再実装しました。次のようになります。
void RpcServer::incomingConnection(int socketDescriptor){
QThread *thread = new QThread();
RpcServerConnection *client = new RpcServerConnection(socketDescriptor);
client->moveToThread(thread);
connect(thread, SIGNAL(started()), client, SLOT(init()));
connect(client, SIGNAL(finish()), thread, SLOT(quit()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}
RpcServerConnection によって管理されるデータ交換。初期化メソッドは次のようになります。
void RpcServerConnection::init(){
qDebug() << "ServerSocket(" << QThread::currentThreadId() << "): Init";
clientConnection = new QTcpSocket();
clientConnection->setSocketDescriptor(socketDescriptor);
connect(clientConnection, SIGNAL(readyRead()), this, SLOT(readFromSocket()));
connect(clientConnection, SIGNAL(disconnected()), this, SLOT(deleteLater()));
connect(this, SIGNAL(finish()), this, SLOT(deleteLater()));
}
すべてのデータが受信され、応答が送信されると、finish シグナルが発行されます。デバッグ すべてのスレッドとソケットが削除されていることがわかります。ただし、プロセスメモリは新しい接続ごとに増加し、終了しても解放されません...
QTcpServer から継承する場合、何か他のものを解放する必要がありますか?