• 6923阅读
  • 7回复

请教高手指点 QTcpServer 应用的问题,出现内存泄漏 [复制链接]

上一主题 下一主题
离线hxj830088
 
只看楼主 倒序阅读 楼主  发表于: 2009-12-23
— 本帖被 XChinux 从 General Qt Programming 移动到本区(2011-01-02) —
请教高手指点 QTcpServer 应用的问题,出现内存泄漏
测试平台为XP系统:
我的思想如下:自己重继承了 QTcpServer 类,
重定义了函数void  incomingConnection(int socketDescriptor),
在此函数中自己定义了一个信号,为了获得socketDescriptor的值,
以后自己创建 QTcpSocket 连接时用。把QTcpSocket从QTcpServer中拿出来。
新类名为 myQTcpServer.
创建了自己的类 myQTcpServer 的一个对象,然后总是监听本机的端口9999,
如果有新的连接,就读取新连接的IP和端口号进行显示,然后断开连接。
当客户端,连接次数多了以后,大约几十次,发现程序的内存会慢慢的一直变大。
不知道为什么,有什么方法,可以解决此问题。希望高手指点,在此先谢谢了!
完整测试代码如下:
//主程序
//================================================
//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
//对 QTcpServer 类扩展了一个信号
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;     //端口
 //建立一个自己类的TCP服务
 tcpserver=new myQTcpServer;
 //启用监听端口
 bool tcplisten;
 tcplisten=tcpserver->listen(QHostAddress::Any,port);
 if(tcplisten)
 {
  lineEditText->setText("listened!");
  connect(tcpserver,SIGNAL(mysignalincomingConnection(int)),this,SLOT(slotincomingConnection(int)));
 }
 else
 {
  lineEditText->setText("error!");
  return;
 }
}
//有客户端连接过来
void mytesttcpserver :: slotincomingConnection(int socketDescriptor)
{
 QTcpSocket testTcpSocket;
 testTcpSocket.setSocketDescriptor(socketDescriptor);
 //显示连接次数,远程主机IP和端口
 socket_count++;
 QVariant vcount(socket_count);
 QVariant vport(testTcpSocket.peerPort());
 QString msg;
 msg="count:"+vcount.toString()+" IP:"+testTcpSocket.peerAddress().toString()+"  port:"+vport.toString();
 lineEditText->setText(msg);
 testTcpSocket.disconnectFromHost();
 testTcpSocket.close();
 testTcpSocket.abort();
}
//========================================================

离线318065268
只看该作者 1楼 发表于: 2009-12-23
楼主手头的资料显然不全啊,做server的话一些教材里面有示例代码的,感觉比较标准,这里贴出来,参考一下:
void UiServer::acceptConnection()
{
    qDebug()<<"new connect come!";
    serverConnect = serverListen->nextPendingConnection();

    connect(serverConnect,SIGNAL(readyRead()),this,SLOT(dealMessage()));
    connect(serverConnect, SIGNAL(error(QAbstractSocket::SocketError)),
            this, SLOT(displayError(QAbstractSocket::SocketError)));

    ui->statusLabel->setText(tr("Connection come and accept."));
    //serverListen->close();
}

头文件里面声明:
  QTcpServer * serverListen;
    QTcpSocket *serverConnect;

其中,关键的函数是“nextPendingConnection()”,使用该函数就能实现楼主所说的:“为了获得socketDescriptor的值,以后自己创建 QTcpSocket 连接时用。把QTcpSocket从QTcpServer中拿出来”。
Email  rsail@126.com(私人邮箱)
QQ:   318065268
离线yj_yulin

只看该作者 2楼 发表于: 2009-12-23
vs2008 + qt4.6用如下python脚本测试,没有问题(debug模式)
import socket
loop_count= 100000
ip = '127.0.0.1'
port = 9999
while loop_count > 0:
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    s.connect((ip,port))
    s.send('abc')
    loop_count = loop_count - 1
离线hxj830088
只看该作者 3楼 发表于: 2009-12-23
我用的是QT4.5.3,没有用QT4.6测试过。我是在xp系统上测试过,看到程序的内存和虚拟内存的总和会增加的!!
离线hxj830088
只看该作者 4楼 发表于: 2009-12-23
谢谢1,2楼的楼主!!
离线yangfanxing
只看该作者 5楼 发表于: 2009-12-23
引用第4楼hxj830088于2009-12-23 17:53发表的  :
谢谢1,2楼的楼主!!

老大。。。
这个楼主只能是你啊~~~1楼的是沙发,2楼的是板凳。。。我坐台阶。
PHPWind好恶心。。。不想看这种界面。。。
离线hxj830088
只看该作者 6楼 发表于: 2009-12-24
只要提建议的,我都支持,都非常感谢!!!我用了QT4.6版本,进行编译,然后用debug模式调试,确实不再出现内存泄漏问题! 5楼说的在理啊,小弟以后说话会注意分寸!!!
离线benbenmajia

只看该作者 7楼 发表于: 2009-12-24
LZ真是.....我没的坐了,看看别人的帖子总是有长进的
安然.....
快速回复
限100 字节
 
上一个 下一个