Results 1 to 3 of 3

Thread: Qt socket consumes memory after transmitting

  1. #1
    Join Date
    Feb 2011
    Posts
    31
    Thanks
    23
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation Qt socket consumes memory after transmitting

    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
    Qt Code:
    1. main ()
    2. {
    3. /***** TO OTHER PROCESS *******************************************************/
    4. m_keysServerTcpSocket = new(QTcpSocket);
    5.  
    6. /****** FROM OTHER PROCESS ****************************************************/
    7. m_tcpKeysFromServer = new QTcpServer(this);
    8. connect(m_tcpKeysFromServer, SIGNAL(newConnection()), this, SLOT(acceptConnectionFromKeysServer()));
    9.  
    10. serverListenFromKeysServer();
    11. }
    12.  
    13. /************** CODE USED TO TRANSMITT A MESSAGGE **********************************************************************/
    14.  
    15. // this function send a message to other process
    16. void ecmqMfgClient::sendToKeysServer()
    17. {
    18.  
    19. m_keysServerTcpSocket->connectToHost(m_Params.IpAddressKeysServer, m_Params.PortToKeysServer);
    20.  
    21. connect(m_keysServerTcpSocket, SIGNAL(disconnected()),
    22. this, SLOT(checkDisconnect()));
    23.  
    24. QByteArray block;
    25. QDataStream out(&block, QIODevice::WriteOnly);
    26. out.setVersion(QDataStream::Qt_4_1);
    27.  
    28. //1.
    29. out << (quint16)0;
    30.  
    31. //2. port of client
    32. out << m_Params.PortFromKeysServer;
    33.  
    34. //3.
    35. out << cmd;
    36.  
    37. //4. type of server request
    38. out << type;
    39.  
    40. //5. type of server request
    41. out << fileName;
    42.  
    43. //6. length_payload
    44.  
    45. quint16 length_payload = 0;
    46. length_payload = (quint16)(block.size() + sizeof(quint16));
    47. out << length_payload;
    48.  
    49. m_keysServerTcpSocket->write(block);
    50. m_keysServerTcpSocket->disconnectFromHost();
    51. m_keysServerTcpSocket->waitForDisconnected(10000);
    52.  
    53. }
    54.  
    55.  
    56. void ecmqMfgClient::checkDisconnect()
    57. {
    58. m_keysServerTcpSocket->flush();
    59. //m_keysServerTcpSocket->abort();
    60. m_keysServerTcpSocket->close();
    61.  
    62. qDebug("checkDisconnect() OK!!");
    63. }
    64.  
    65.  
    66.  
    67. /************** CODE USED TO RECEIVE A MESSAGE **************************************************************************/
    68.  
    69. void ecmqMfgClient::acceptConnectionFromKeysServer()
    70. {
    71. m_tcpKeysFromServerConnection = m_tcpKeysFromServer->nextPendingConnection();
    72. connect(m_tcpKeysFromServerConnection, SIGNAL(readyRead()),
    73. this, SLOT(receiveFromKeysServer()));
    74. connect(m_tcpKeysFromServerConnection, SIGNAL(error(QAbstractSocket::SocketError)),
    75. this, SLOT(displayErrorFromKeyServer(m_tcpKeysFromServerConnection, QAbstractSocket::SocketError)));
    76.  
    77. }
    78.  
    79. void ecmqMfgClient::receiveFromKeysServer()
    80. {
    81. QString msg;
    82.  
    83. qint16 bytesAvail;
    84. bytesAvail= m_tcpKeysFromServerConnection->bytesAvailable();
    85. if (bytesAvail > 0)
    86. {
    87. // QDataStream in(this);
    88. QDataStream pin(m_tcpKeysFromServerConnection);
    89. pin.setVersion(QDataStream::Qt_4_1);
    90.  
    91. //.1
    92. quint16 cmd;
    93. pin >> cmd;
    94.  
    95. //.2
    96. quint16 type;
    97. pin >> type;
    98.  
    99. //.3
    100. QString keyCode;
    101. pin >> keyCode;
    102.  
    103. //.4
    104. quint16 length_payload;// = 0;
    105. pin >> length_payload;
    106.  
    107. QString msg = tr(">>receiveFromKeysServer<< ")
    108. + tr (" > bytesAvail ") + QString("%5").arg(bytesAvail) + tr (" < ")
    109. + tr (" > -- cmd ") + QString("%5").arg(cmd) + tr (" < ")
    110. + tr (" > -- type ") + QString("%5").arg(type) + tr (" < ")
    111. + tr (" > -- keyCode ") + keyCode + tr (" < ")
    112. + tr (" > -- length_payload ") + QString("%5").arg(length_payload) + tr (" < ")
    113. + tr("\n");
    114.  
    115. m_tcpKeysFromServerConnection->flush();
    116.  
    117. serverListenFromKeysServer();
    118. }
    119.  
    120.  
    121.  
    122. void ecmqMfgClient::serverListenFromKeysServer()
    123. {
    124. QString newLabelSvrName;
    125. QPalette palette;
    126.  
    127. if (!m_tcpKeysFromServer->isListening() &&
    128. !m_tcpKeysFromServer->listen(QHostAddress(m_localIPAddress), m_Params.PortFromKeysServer))
    129. {
    130. newLabelSvrName = tr("Unable to start KEYS server: check IP address!");
    131. m_netInfo->setLabelFromKeysServer(newLabelSvrName);
    132.  
    133.  
    134. const QString msg = tr("Unable to start KEYS server at ")
    135. + m_localIPAddress + " : " + QString("%5").arg(m_Params.PortFromKeysServer)
    136. + tr(" on ") + QDateTime::currentDateTime().toString(Qt::ISODate)
    137. + tr("\n");
    138.  
    139. }
    140. else
    141. {
    142. // if tcpServer listen to the provided ipAddr and ipPort, the newConnection
    143. // signal is emitted every time a client tries to connect
    144. newLabelSvrName = tr("Listening from ")
    145. + m_Params.IpAddressKeysServer +" @ " + QString("%5").arg(m_Params.PortFromKeysServer) + " from KEYS srv";
    146.  
    147. }
    148.  
    149. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt socket consumes memory after transmitting

    One thing is for sure -- you are not deleting the socket anywhere.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    marco.stanzani (26th March 2012)

  4. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Qt socket consumes memory after transmitting

    Also assuming that the packet that is sent arrives in one chunk at the other end.

  5. The following user says thank you to ChrisW67 for this useful post:

    marco.stanzani (28th March 2012)

Similar Threads

  1. Replies: 2
    Last Post: 22nd May 2011, 21:31
  2. qt TCP socket
    By ketan in forum Qt Programming
    Replies: 3
    Last Post: 20th November 2009, 11:17
  3. bug in Qt socket
    By dognzhe in forum Qt Programming
    Replies: 7
    Last Post: 12th June 2009, 07:36
  4. TCP Socket with Qt4.2
    By Walsi in forum Newbie
    Replies: 3
    Last Post: 20th September 2007, 05:35
  5. bad UDP socket :(
    By chaosgeorge in forum Qt Programming
    Replies: 1
    Last Post: 11th December 2006, 02:55

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.