hxj830088
23rd December 2009, 10:16
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
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