About the question of QTcpServer, there memory leak
About the question of QTcpServer, there memory leak :(
Test platform : the system of xp
When a client connects about forty times, the program's memory will gradually become bigger.
Want to help, thank you!
The complete code is as follows:
//================================================
//main.cpp
#include <QtGui/QApplication>
#include "tcpserver.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
mytesttcpserver tcp;
tcp.show();
return a.exec();
}
//================================================
//tcpserver.h
#ifndef TCPSERVER_H
#define TCPSERVER_H
#include <QtGui>
#include <QDialog>
#include <QTcpServer>
QT_BEGIN_NAMESPACE
class QString;
class QLabel;
class QLineEdit;
//net
class QHostInfo;
class QHostAddress;
class QNetworkAddressEntry;
class QTcpSocket;
QT_END_NAMESPACE
class myQTcpServer :public QTcpServer
{
Q_OBJECT
public:
myQTcpServer(QObject *parent = 0);
signals:
void mysignalincomingConnection(int socketDescriptor);
protected:
void incomingConnection(int socketDescriptor);
};
//tcp server
class mytesttcpserver :public QDialog
{
Q_OBJECT
public:
mytesttcpserver(QWidget *parent = 0);
public slots:
void slotincomingConnection(int socketDescriptor);
private:
QLabel *label;
QLineEdit *lineEditText;
myQTcpServer *tcpserver;
quint16 port;
int socket_count;
};
#endif // TCPSERVER_H
//================================================
//tcpserver.cpp
#include "tcpserver.h"
#include <QString>
#include <QDebug>
#include <QList>
#include <QVBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QVariant>
//net
#include <QHostInfo>
#include <QHostAddress>
#include <QNetworkAddressEntry>
#include <QNetworkInterface>
#include <QTcpSocket>
#include <QTcpServer>
myQTcpServer :: myQTcpServer(QObject *parent)
:QTcpServer(parent)
{
;
}
void myQTcpServer :: incomingConnection(int socketDescriptor)
{
emit mysignalincomingConnection(socketDescriptor);
}
mytesttcpserver :: mytesttcpserver(QWidget *parent)
:QDialog(parent)
{
setWindowTitle(tr("TCP server Test"));
QVBoxLayout *vbMain=new QVBoxLayout(this);
label=new QLabel(this);
label->setText(tr("Client Info:"));
vbMain->addWidget(label);
lineEditText=new QLineEdit(this);
vbMain->addWidget(lineEditText);
this->resize(300,100);
socket_count=0;
port=9999;
tcpserver=new myQTcpServer;
bool tcplisten;
tcplisten=tcpserver->listen(QHostAddress::Any,port);
if(tcplisten)
{
lineEditText->setText("listened!");
connect(tcpserver,SIGNAL(mysignalincomingConnectio n(int)),this,SLOT(slotincomingConnection(int)));
}
else
{
lineEditText->setText("error!");
}
}
void mytesttcpserver :: slotincomingConnection(int socketDescriptor)
{
QTcpSocket testTcpSocket;
testTcpSocket.setSocketDescriptor(socketDescriptor );
socket_count++;
QString msg;
msg=QString("count:%1").arg(socket_count,0,10);
msg=msg+" IP:"+testTcpSocket.peerAddress().toString()+" port:"+vport.toString();
lineEditText->setText(msg);
testTcpSocket.disconnectFromHost();
testTcpSocket.close();
testTcpSocket.abort();
}
//================================================== ======
//code end
Re: About the question of QTcpServer, there memory leak
How does your program meant to work?
Does a new dialog gets created for each new connection?
Can there be multiple connections (and dialogs) running at the same time?
I see that in your code:
Code:
tcpserver=new myQTcpServer;
is being allocated for each dialog, but it never gets destroyed - so this IS a memory leak for sure.
You can fix this either by giving your tcpserver a parent, or, by manually deleting it in the dialog destructor.
Re: About the question of QTcpServer, there memory leak
Quote:
The complete code is as follows:
and use CODE tags for pasting code!
Re: About the question of QTcpServer, there memory leak
Quote:
and use CODE tags for pasting code!
heh... if I had a cent for each time this line was appropriate.... :rolleyes:
Re: About the question of QTcpServer, there memory leak
The problem of memory leak has been solved, thank you!
The memory leak problem does not occur with the version of QT4.6.
the version of QT4.5.3 has this problem.