PDA

View Full Version : Qt socket consumes memory after transmitting



marco.stanzani
26th March 2012, 09:38
hello forum
i have an issue qith Qtcpsocke C/S communication. It looks like the sockets used to transmit / receive allocate memory without releasing it, making the application to crash after a while (this depends on the amount of memory the PC running the program )

Below a small test case i have build from the large application which hope somebody be so patient to have a look into

- in order to transmit a message block the method sendToKeysServer() is called

- the receiving process get the message and transmit the response, which is acquired by the function acceptConnectionFromKeysServer(), which will call the receiveFromKeysServer(), in charge of extracting the message contents

Please consider that everytime the following activity are exectude
- m_keysServerTcpSocket->write(block)
- m_tcpKeysFromServerConnection = m_tcpKeysFromServer->nextPendingConnection();
the memory allocated by the application is growing (i can easily see this by checking he windows XP task manager): it seem that the socket are 'eating memory and i cannot find a way to release it :( (i tried flush, abort, closed... no way!)

does anybody see anything wrong with this code? I used the fortune client server application, but not exactly as inthe example ...

I am using Qt 4.7.1: may help updating to 4.8?

thanks in advance for any suggestion

ciao


main ()
{
/***** TO OTHER PROCESS ************************************************** *****/
m_keysServerTcpSocket = new(QTcpSocket);

/****** FROM OTHER PROCESS ************************************************** **/
m_tcpKeysFromServer = new QTcpServer(this);
connect(m_tcpKeysFromServer, SIGNAL(newConnection()), this, SLOT(acceptConnectionFromKeysServer()));

serverListenFromKeysServer();
}

/************** CODE USED TO TRANSMITT A MESSAGGE ************************************************** ********************/

// this function send a message to other process
void ecmqMfgClient::sendToKeysServer()
{

m_keysServerTcpSocket->connectToHost(m_Params.IpAddressKeysServer, m_Params.PortToKeysServer);

connect(m_keysServerTcpSocket, SIGNAL(disconnected()),
this, SLOT(checkDisconnect()));

QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_1);

//1.
out << (quint16)0;

//2. port of client
out << m_Params.PortFromKeysServer;

//3.
out << cmd;

//4. type of server request
out << type;

//5. type of server request
out << fileName;

//6. length_payload

quint16 length_payload = 0;
length_payload = (quint16)(block.size() + sizeof(quint16));
out << length_payload;

m_keysServerTcpSocket->write(block);
m_keysServerTcpSocket->disconnectFromHost();
m_keysServerTcpSocket->waitForDisconnected(10000);

}


void ecmqMfgClient::checkDisconnect()
{
m_keysServerTcpSocket->flush();
//m_keysServerTcpSocket->abort();
m_keysServerTcpSocket->close();

qDebug("checkDisconnect() OK!!");
}



/************** CODE USED TO RECEIVE A MESSAGE ************************************************** ************************/

void ecmqMfgClient::acceptConnectionFromKeysServer()
{
m_tcpKeysFromServerConnection = m_tcpKeysFromServer->nextPendingConnection();
connect(m_tcpKeysFromServerConnection, SIGNAL(readyRead()),
this, SLOT(receiveFromKeysServer()));
connect(m_tcpKeysFromServerConnection, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(displayErrorFromKeyServer(m_tcpKeysFromServer Connection, QAbstractSocket::SocketError)));

}

void ecmqMfgClient::receiveFromKeysServer()
{
QString msg;

qint16 bytesAvail;
bytesAvail= m_tcpKeysFromServerConnection->bytesAvailable();
if (bytesAvail > 0)
{
// QDataStream in(this);
QDataStream pin(m_tcpKeysFromServerConnection);
pin.setVersion(QDataStream::Qt_4_1);

//.1
quint16 cmd;
pin >> cmd;

//.2
quint16 type;
pin >> type;

//.3
QString keyCode;
pin >> keyCode;

//.4
quint16 length_payload;// = 0;
pin >> length_payload;

QString msg = tr(">>receiveFromKeysServer<< ")
+ tr (" > bytesAvail ") + QString("%5").arg(bytesAvail) + tr (" < ")
+ tr (" > -- cmd ") + QString("%5").arg(cmd) + tr (" < ")
+ tr (" > -- type ") + QString("%5").arg(type) + tr (" < ")
+ tr (" > -- keyCode ") + keyCode + tr (" < ")
+ tr (" > -- length_payload ") + QString("%5").arg(length_payload) + tr (" < ")
+ tr("\n");

m_tcpKeysFromServerConnection->flush();

serverListenFromKeysServer();
}



void ecmqMfgClient::serverListenFromKeysServer()
{
QString newLabelSvrName;
QPalette palette;

if (!m_tcpKeysFromServer->isListening() &&
!m_tcpKeysFromServer->listen(QHostAddress(m_localIPAddress), m_Params.PortFromKeysServer))
{
newLabelSvrName = tr("Unable to start KEYS server: check IP address!");
m_netInfo->setLabelFromKeysServer(newLabelSvrName);


const QString msg = tr("Unable to start KEYS server at ")
+ m_localIPAddress + " : " + QString("%5").arg(m_Params.PortFromKeysServer)
+ tr(" on ") + QDateTime::currentDateTime().toString(Qt::ISODate)
+ tr("\n");

}
else
{
// if tcpServer listen to the provided ipAddr and ipPort, the newConnection
// signal is emitted every time a client tries to connect
newLabelSvrName = tr("Listening from ")
+ m_Params.IpAddressKeysServer +" @ " + QString("%5").arg(m_Params.PortFromKeysServer) + " from KEYS srv";

}

}

wysota
26th March 2012, 10:09
One thing is for sure -- you are not deleting the socket anywhere.

ChrisW67
26th March 2012, 11:38
Also assuming that the packet that is sent arrives in one chunk at the other end.