PDA

View Full Version : QTcpSocket connectToHost memory leak?



Lele
3rd March 2006, 18:20
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.



bool ret;
QTcpSocket m_Socket;
m_Socket.connectToHost("127.0.0.1", 740);
ret=m_Socket.waitForConnected();
m_Socket.disconnectFromHost();
ret=m_Socket.waitForDisconnected();
QAbstractSocket::SocketError se;
se=m_Socket.error();

m_Socket.abort();
m_Socket.close();



Any help is appreciated.
Thanks in advance

wysota
3rd March 2006, 18:32
If you let that object go out of scope (or delete it if it is deletable), then all resources should be freed.

Lele
3rd March 2006, 18:43
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



void QSendAlarm::SendEndAlarm()
{
bool ret;
QTcpSocket mySocket;
mySocket.connectToHost("127.0.0.1", 740);
ret=mySocket.waitForConnected();
mySocket.disconnectFromHost();
ret=mySocket.waitForDisconnected();
QAbstractSocket::SocketError se;
se=mySocket.error();

mySocket.abort();
mySocket.close();
}


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

wysota
3rd March 2006, 18:52
Hard to say... Did you try to use Valgrind or a simmilar tool to check for leaks?

Lele
3rd March 2006, 19:04
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



ret=mySocket.waitForDisconnected();


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

wysota
3rd March 2006, 19:19
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?

Lele
6th March 2006, 08:57
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

Lele
6th March 2006, 09:46
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

wysota
6th March 2006, 10:11
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 (http://www.trolltech.com/developer/tasktracker).

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