Results 1 to 2 of 2

Thread: Memory leak in Qsocketserver

  1. #1
    Join Date
    Aug 2008
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Memory leak in Qsocketserver

    I used valgrind to locate memory leaks .
    I use the standard QT socket
    Qt Code:
    1. /** ---------------------------------------------------------------------------
    2.  * @brief socket_client::socket_client Constructor
    3.  */
    4. socket_client::socket_client(void)
    5. {
    6. queue_array = new (QByteArray);
    7. socket_connected = false;
    8. tcp_socket = new QTcpSocket(this);
    9. disconnect(tcp_socket ,0 ,0 ,0);
    10.  
    11. connect(&my_heartbeat_timer, SIGNAL(timeout()), this, SLOT(slot_heartbeat_timer()));
    12. change_state = true;
    13. my_port = 0;
    14. }
    15. /** ---------------------------------------------------------------------------
    16.  * @brief socket_client::~socket_client Destructor
    17.  */
    18. socket_client::~socket_client(void)
    19. {
    20. if (queue_array != NULL) delete queue_array;
    21. if (tcp_socket != NULL) tcp_socket->deleteLater();
    22. }
    To copy to clipboard, switch view to plain text mode 

    The code with " if(tcp_socket->waitForConnected(200))" is highlighted by valgrind.

    Qt Code:
    1. /** ---------------------------------------------------------------------------
    2.  * @brief socket_client::connect_to_server
    3.  * @param ip_address
    4.  * @param port
    5.  */
    6. BOOL socket_client::connect_to_server(const char * ip_address,
    7. const UINT_32 & port)
    8. {
    9. change_state = socket_connected;
    10. my_ip_address = ip_address;
    11. my_port = port;
    12.  
    13. my_heartbeat_timer.stop();
    14.  
    15. //socket_connected = false;
    16.  
    17. tcp_socket->connectToHost(my_ip_address, my_port);
    18. if(tcp_socket->waitForConnected(200))
    19. {
    20. socket_connected = true;
    21.  
    22. // Disconnect all slots
    23. tcp_socket->disconnect();
    24. disconnect(tcp_socket ,0 ,0 ,0);
    25.  
    26. emit signal_connected(true);
    27.  
    28. // re-connect signals and slots.
    29. connect(tcp_socket,SIGNAL(readyRead()),this,SLOT(read_data()));
    30. connect(tcp_socket,SIGNAL(disconnected()),this,SLOT(slot_retry_server_connect()));
    31.  
    32. if (change_state != socket_connected )
    33. {
    34. change_state = socket_connected;
    35. gprintf ("\n> Connected to SocServer %s:%d ",
    36. my_ip_address.toLatin1().data(),
    37. my_port);
    38. }
    39.  
    40. return true;
    41. }
    42. else
    43. {
    44. socket_connected = false;
    45.  
    46. if (change_state != socket_connected )
    47. {
    48. // only print error.e message once
    49. change_state = socket_connected;
    50. gprintf ("\n SocServer connect FAILED [%s] Issue reconnect to %s:%d ",
    51. tcp_socket->errorString().toLatin1().data(),
    52. my_ip_address.toLatin1().data(),
    53. my_port);
    54. }
    55.  
    56. emit signal_connected(false);
    57.  
    58. disconnect(tcp_socket ,0 ,0 ,0);
    59. tcp_socket->disconnect();
    60. tcp_socket->close();
    61.  
    62. QTimer::singleShot(5000, this, SLOT(slot_retry_server_connect()));
    63. }
    64. return false;
    65. }
    To copy to clipboard, switch view to plain text mode 
    Attached valgrind 'report'
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,316
    Thanks
    315
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Memory leak in Qsocketserver

    It is unlikely that it is a real memory leak. It probably results from valgrind not understanding Qt's parent-child lifetime management rules.

    The lines inside your destructor:

    Qt Code:
    1. socket_client::~socket_client(void)
    2. {
    3. if (queue_array != NULL) delete queue_array;
    4. if (tcp_socket != NULL) tcp_socket->deleteLater();
    5. }
    To copy to clipboard, switch view to plain text mode 

    should be replaced with:

    Qt Code:
    1. socket_client::~socket_client(void)
    2. {
    3. delete queue_array;
    4. }
    To copy to clipboard, switch view to plain text mode 

    because:

    1 - if you have initialized queue_array to NULL in your constructor or have allocated it using operator new(), then there is no need to check for NULL before deleting because operator delete() is perfectly happy to accept a NULL pointer and will do nothing.

    2 - You do not need to delete tcp_socket or call deleteLater() on it because you constructed is as a child of socket_client, and it will go out of scope when the socket_client instance goes out of scope.

Similar Threads

  1. Is there a memory leak here?
    By vinnzcrafts in forum Newbie
    Replies: 2
    Last Post: 19th March 2014, 14:39
  2. Qt & Memory Leak
    By fanoI in forum Qt Programming
    Replies: 7
    Last Post: 31st October 2013, 08:53
  3. Qt example with memory leak
    By Squall in forum Qt Programming
    Replies: 5
    Last Post: 21st February 2011, 10:07
  4. memory leak
    By mattia in forum Newbie
    Replies: 18
    Last Post: 16th January 2008, 10:22
  5. Memory leak
    By zlatko in forum Qt Programming
    Replies: 8
    Last Post: 28th March 2006, 19:02

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
  •  
Qt is a trademark of The Qt Company.