Results 1 to 10 of 10

Thread: QTcpSocket connectToHost memory leak?

  1. #1
    Join Date
    Jan 2006
    Posts
    122
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTcpSocket connectToHost memory leak?

    Hi all,
    I'm trying to use QTcpSocket to connect to a server, with the code below I noticed an increase in the number of handles allocated everytime I perform connectToHost not realesed with disconnect ot close.
    Am I doing something wrong?
    Basically I need to create a new connection every time I need to contact the server, so I need to connect several times during the life of my software.

    Qt Code:
    1. bool ret;
    2. QTcpSocket m_Socket;
    3. m_Socket.connectToHost("127.0.0.1", 740);
    4. ret=m_Socket.waitForConnected();
    5. m_Socket.disconnectFromHost();
    6. ret=m_Socket.waitForDisconnected();
    7. QAbstractSocket::SocketError se;
    8. se=m_Socket.error();
    9.  
    10. m_Socket.abort();
    11. m_Socket.close();
    To copy to clipboard, switch view to plain text mode 


    Any help is appreciated.
    Thanks in advance

  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: QTcpSocket connectToHost memory leak?

    If you let that object go out of scope (or delete it if it is deletable), then all resources should be freed.

  3. #3
    Join Date
    Jan 2006
    Posts
    122
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket connectToHost memory leak?

    Thanks for answering,
    Well apart for the misleading m_ at the beginning that variable is non memeber,
    what I do is call this method whenever I have to send a message to a TcpServer

    Qt Code:
    1. void QSendAlarm::SendEndAlarm()
    2. {
    3. bool ret;
    4. QTcpSocket mySocket;
    5. mySocket.connectToHost("127.0.0.1", 740);
    6. ret=mySocket.waitForConnected();
    7. mySocket.disconnectFromHost();
    8. ret=mySocket.waitForDisconnected();
    9. QAbstractSocket::SocketError se;
    10. se=mySocket.error();
    11.  
    12. mySocket.abort();
    13. mySocket.close();
    14. }
    To copy to clipboard, switch view to plain text mode 

    So the variable goes out of scope everytime, but the handles increase everytime I call connectToHost, is that a leak or I'm missing something?

    Thanks again
    bye

  4. #4
    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: QTcpSocket connectToHost memory leak?

    Hard to say... Did you try to use Valgrind or a simmilar tool to check for leaks?

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

    Lele (3rd March 2006)

  6. #5
    Join Date
    Jan 2006
    Posts
    122
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket connectToHost memory leak?

    I'm developing under Windows, and I can notice the handles increase in the Task Manager, also virtual memory, what I discovered is that is that

    Qt Code:
    1. ret=mySocket.waitForDisconnected();
    To copy to clipboard, switch view to plain text mode 

    return false with QAbstractSocket::UnknownSocketError, is there the problem?

  7. #6
    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: QTcpSocket connectToHost memory leak?

    There are Valgrind-like tools for Windows too.

    About the error -- it depends on the socket state. if you skip the optional parameter, there is a 30 second timeout -- the socket will not wait any longer. You'd have to monitor the state of the socket. And does this happen every time you use QTcpSocket? Have you tried using it on a different machine?

  8. #7
    Join Date
    Jan 2006
    Posts
    122
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket connectToHost memory leak?

    I only used it on Windows, but I can't believe it's bug in QTcpSocket.
    I think that using a different tool for checking the handles wouldn't help, considering the case is very simple.
    Maybe I miss some inizialization?
    Is the code I posted safe and clean? Do I have to check some asyncrounous signal?
    How is the right way to open a connection? I think mine is the same as the one in the example (Fortune Client)
    Thanks again

  9. #8
    Join Date
    Jan 2006
    Posts
    122
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket connectToHost memory leak?

    Well, actually I've just dicovered that the leak is in the fortuneclient example too.
    I have Qt 4.1.0 and Windows Xp.
    Bye

  10. #9
    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: QTcpSocket connectToHost memory leak?

    Unless you check if this is really a leak, you won't be sure of that. And leak-checking tools do that you know.

    If you are convinced this is a bug, you can report it to Trolltech using their tasktracker.

  11. #10
    Join Date
    Jan 2006
    Posts
    122
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket connectToHost memory leak?

    Thanks for the tip, I'm quite new to Qt,
    I found the same bug already listed in tasktracker:
    http://www.trolltech.com/developer/t...entry&id=92125
    Well, it shoud be fixed even in my version 4.1.0, I'll try upgrading to 4.1.1
    Bye

Similar Threads

  1. Memory leak detection
    By Sid in forum Qt Programming
    Replies: 8
    Last Post: 4th May 2011, 22:38
  2. memory leak question
    By cool_qt in forum General Programming
    Replies: 3
    Last Post: 20th January 2009, 07:49
  3. Memory leak weirdness
    By Darhuuk in forum General Programming
    Replies: 10
    Last Post: 10th January 2008, 18:51
  4. QPixMap and Memory leak
    By Krish_ng in forum Qt Programming
    Replies: 1
    Last Post: 7th August 2007, 14:18
  5. Memory Leak in my Application :-(
    By Svaths in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2007, 19:42

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.