Results 1 to 5 of 5

Thread: About the question of QTcpServer, there memory leak

  1. #1
    Join Date
    Dec 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy 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 ublic QTcpServer
    {
    Q_OBJECT

    public:
    myQTcpServer(QObject *parent = 0);
    signals:
    void mysignalincomingConnection(int socketDescriptor);
    protected:
    void incomingConnection(int socketDescriptor);
    };

    //tcp server
    class mytesttcpserver ublic 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

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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:
    Qt Code:
    1. tcpserver=new myQTcpServer;
    To copy to clipboard, switch view to plain text mode 
    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: About the question of QTcpServer, there memory leak

    The complete code is as follows:
    and use CODE tags for pasting code!
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: About the question of QTcpServer, there memory leak

    and use CODE tags for pasting code!
    heh... if I had a cent for each time this line was appropriate....
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Dec 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Smile 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.

Similar Threads

  1. Memory Leak Issue
    By linuxdev in forum Qt Programming
    Replies: 10
    Last Post: 1st December 2009, 15:21
  2. QList<pointer>::takeFirst() and memory leak question
    By AcerExtensa in forum Qt Programming
    Replies: 3
    Last Post: 9th July 2009, 13:20
  3. memory leak question
    By cool_qt in forum General Programming
    Replies: 3
    Last Post: 20th January 2009, 07:49
  4. Memory leak weirdness
    By Darhuuk in forum General Programming
    Replies: 10
    Last Post: 10th January 2008, 18:51
  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.