Results 1 to 1 of 1

Thread: Help with memory mangment or class deconstraction

  1. #1
    Join Date
    May 2011
    Posts
    5
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with memory mangment or class deconstraction

    i writing simple network class but i have some problem.

    program start with 1MB memory use



    when i connect 100 connections


    memroy become 1.5MB use


    when i disconnect 100 connections

    memory become 3MB use


    the problem i think is CClient never get deleted.

    the code
    CPP :
    Qt Code:
    1. #include "TServer.h"
    2. //------------------------------------------------------------------------------------------------------------------------
    3.  
    4. CClient::CClient(CServer * parent, int handle)
    5. {
    6. m_parent = parent;
    7. m_Handle = handle;
    8. }
    9. //------------------------------------------------------------------------------------------------------------------------
    10.  
    11. void CClient::run()
    12. {
    13. m_Socket = new QTcpSocket();
    14. // ----
    15. if(m_Socket->setSocketDescriptor(m_Handle) == false)
    16. {
    17. m_parent->onError(__FUNCTION__, m_Socket->errorString());
    18. }
    19. else
    20. {
    21. connect(m_Socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
    22. connect(m_Socket, SIGNAL(readyRead()), this, SLOT(readAble()));
    23. connect(m_Socket, SIGNAL(destroyed()), this, SLOT(dead()));
    24. }
    25. // ----
    26. m_parent->onConnect(m_Handle);
    27. }
    28. //------------------------------------------------------------------------------------------------------------------------
    29.  
    30. void CClient::dead()
    31. {
    32. printf("dead \n"); // never called
    33. }
    34. //------------------------------------------------------------------------------------------------------------------------
    35.  
    36. void CClient::disconnected()
    37. {
    38. m_parent->onDisconnect(m_Handle);
    39.  
    40. }
    41. //------------------------------------------------------------------------------------------------------------------------
    42.  
    43. void CClient::readAble()
    44. {
    45. m_parent->onRecv(m_Handle, m_Socket->readAll());
    46. }
    47. //------------------------------------------------------------------------------------------------------------------------
    48.  
    49. CServer::CServer(QObject * parent) : QTcpServer(parent)
    50. {
    51. m_PoolThreadsCount = 4;
    52. }
    53. //------------------------------------------------------------------------------------------------------------------------
    54.  
    55. void CServer::run(ushort port)
    56. {
    57. bool bReturn = false;
    58. // ----
    59. static QString functionName = __FUNCTION__;
    60. // ----
    61. if(listen(QHostAddress::Any, port) == true)
    62. {
    63. onInfo(functionName, QString("Server is listening on port %0").arg(port));
    64. // ----
    65. bReturn = true;
    66. }
    67. else
    68. {
    69. onInfo(functionName, QString("The server can not run. port %0, %1").arg(port).arg(errorString()));
    70. }
    71. // ----
    72. m_ThreadPoolMgr = new QThreadPool(this);
    73. m_ThreadPoolMgr->setMaxThreadCount(m_PoolThreadsCount);
    74. }
    75. //------------------------------------------------------------------------------------------------------------------------
    76.  
    77. void CServer::onConnect(int handle)
    78. {
    79. onInfo(__FUNCTION__, QString("new connection from handle %0").arg(handle));
    80. }
    81. //------------------------------------------------------------------------------------------------------------------------
    82.  
    83. void CServer::onDisconnect(int handle)
    84. {
    85. onInfo(__FUNCTION__, QString("disconnection from handle %0").arg(handle));
    86. }
    87. //------------------------------------------------------------------------------------------------------------------------
    88.  
    89. void CServer::onRecv(int handle, QByteArray packet)
    90. {
    91. onInfo(__FUNCTION__, QString("recv from handle %0").arg(handle));
    92. }
    93. //------------------------------------------------------------------------------------------------------------------------
    94.  
    95. void CServer::onError(QString function, QString message)
    96. {
    97. printf("[%s][%s]\n", function.toStdString().c_str(), message.toStdString().c_str());
    98. }
    99. //------------------------------------------------------------------------------------------------------------------------
    100.  
    101. void CServer::onInfo(QString function, QString message)
    102. {
    103. printf("[%s][%s]\n", function.toStdString().c_str(), message.toStdString().c_str());
    104. }
    105. //------------------------------------------------------------------------------------------------------------------------
    106.  
    107. void CServer::onWrong(QString function, QString message)
    108. {
    109. printf("[%s][%s]\n", function.toStdString().c_str(), message.toStdString().c_str());
    110. }
    111. //------------------------------------------------------------------------------------------------------------------------
    112.  
    113. void CServer::incomingConnection(int handle)
    114. {
    115. CClient * newClient = new CClient(this, handle);
    116. // ----
    117. newClient->run();
    118. }
    119. //------------------------------------------------------------------------------------------------------------------------
    To copy to clipboard, switch view to plain text mode 

    Header
    Qt Code:
    1. //------------------------------------------------------------------------------------------------------------------------
    2. #ifndef TSERVER_H
    3. #define TSERVER_H
    4. //------------------------------------------------------------------------------------------------------------------------
    5.  
    6. #include <QString>
    7. #include <QDebug>
    8. #include <QTCPServer>
    9. #include <QTcpSocket>
    10. #include <QAbstractSocket>
    11. #include <QThreadPool>
    12. #include <QRunnable>
    13. //------------------------------------------------------------------------------------------------------------------------
    14.  
    15. #include <list>
    16. //------------------------------------------------------------------------------------------------------------------------
    17.  
    18. class CClient;
    19. class CServer;
    20. //------------------------------------------------------------------------------------------------------------------------
    21.  
    22. #define CLIENT_LIST std::list<CClient *>
    23. //------------------------------------------------------------------------------------------------------------------------
    24.  
    25. class CClient : public QObject
    26. {
    27. Q_OBJECT
    28. private:
    29. CServer * m_parent;
    30. QTcpSocket * m_Socket;
    31. int m_Handle;
    32. public:
    33. explicit CClient (CServer * parent, int handle);
    34. // ----
    35. void run ();
    36. private slots:
    37. void disconnected ();
    38. void readAble ();
    39. void dead();
    40. };
    41. //------------------------------------------------------------------------------------------------------------------------
    42.  
    43. class CServer : public QTcpServer
    44. {
    45. Q_OBJECT
    46. private:
    47. ushort m_PoolThreadsCount;
    48. QThreadPool * m_ThreadPoolMgr;
    49. public:
    50. explicit CServer (QObject * parent = 0);
    51. // ----
    52. void run (ushort port = 1234);
    53. // ----
    54. virtual void onConnect (int handle);
    55. virtual void onDisconnect (int handle);
    56. virtual void onRecv (int handle, QByteArray packet);
    57. // ----
    58. virtual void onInfo (QString function, QString message);
    59. virtual void onError (QString function, QString message);
    60. virtual void onWrong (QString function, QString message);
    61. // ----
    62. void inline setPoolThreadsCount (ushort Count) { m_PoolThreadsCount = Count; };
    63. ushort inline getPoolThreadsCount () { return m_PoolThreadsCount; };
    64. protected:
    65. void incomingConnection (int handle);
    66.  
    67. };
    68. //------------------------------------------------------------------------------------------------------------------------
    69. #endif // TSERVER_H
    70. //------------------------------------------------------------------------------------------------------------------------
    To copy to clipboard, switch view to plain text mode 


    dead function in CCLient never called!
    thanks in advance
    Leo


    Added after 59 minutes:


    ok i fixed the problem using this :
    Qt Code:
    1. void CClient::disconnected()
    2. {
    3. m_parent->onDisconnect(m_Handle);
    4. // ----
    5. deleteLater();
    6. m_Socket->deleteLater();
    7. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by iNewLegend; 30th May 2011 at 18:36.

Similar Threads

  1. accessing static member variables of one class in another class
    By jasonknight in forum General Programming
    Replies: 5
    Last Post: 6th September 2010, 14:53
  2. Abstract base class and inherited class members
    By JovianGhost in forum General Programming
    Replies: 3
    Last Post: 19th March 2010, 12:32
  3. Replies: 3
    Last Post: 27th December 2008, 19:34
  4. Replies: 3
    Last Post: 16th May 2007, 11:07
  5. Replies: 2
    Last Post: 4th May 2006, 19:17

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.