Results 1 to 5 of 5

Thread: Memory raising forever in server thread application

  1. #1
    Join Date
    Jun 2010
    Posts
    100
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Memory raising forever in server thread application

    Hi all!

    I have a server application that has a class inherited from QTcpServer and another that is inherited from the QThread and has a QTcpSocket variable. Lets just say class_tcpServer and class_serverThread.

    Qt Code:
    1. class class_tcpServer : QTcpServer {
    2. Q_OBJECT
    3. public:
    4. class_tcpServer();
    5. virtual ~class_tcpServer();
    6.  
    7. private:
    8. QTcpServer *tcpServer;
    9. class_tcpServer *ServerThread;
    10.  
    11. private slots:
    12. void wait_for_data();
    13. void free_slot(qint16);
    14. };
    To copy to clipboard, switch view to plain text mode 

    the implementation has a slot that activates in case of a connection from the server. the slot looks like this:

    Qt Code:
    1. void class_tcpServer::wait_for_data()
    2. {
    3. cout << "New connection made" << endl;
    4.  
    5. while(tcpServer->hasPendingConnections()){ //This controls pending connections
    6.  
    7. for (i = 0 ; i < max_connections ; i++) //Max connections is the number of sql licenses
    8. {
    9. if (connections[i] == 0)
    10. {
    11. cout << "Found empty slot for new connection" << endl;
    12.  
    13. ServerThread[i] = new EFrwServer_thread();
    14.  
    15. connect(ServerThread[i], SIGNAL(free_connection_slot(qint16)), this, SLOT(free_slot(qint16)));
    16.  
    17.  
    18. ServerThread[i]->tcpSocket = tcpServer->nextPendingConnection();
    19.  
    20. ServerThread[i]->my_connection_number = i;
    21.  
    22. ServerThread[i]->start();
    23.  
    24. connections[i] = 1;
    25. i = max_connections;
    26. }
    27. }
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

    The thread does a query and then send the query in a QByteArray via the tcpSocket. That works just fine. At the end of the thread a signal is emited which will activate the slot free_slot(qint16) which simply sets the license to not used.

    Everything works fine but my problem is that the memory goes up and up (which is fine as I don't know how to delete it).

    How do I delete the object? or can someone tell me other solution?

    thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Memory raising forever in server thread application

    Quote Originally Posted by ruben.rodrigues View Post
    Qt Code:
    1. private:
    2. QTcpServer *tcpServer;
    3. class_tcpServer *ServerThread;
    To copy to clipboard, switch view to plain text mode 
    You might want to delete at least these when destructing. Do some checks first before deleting though!

    How do I delete the object? or can someone tell me other solution?
    Sure:
    Qt Code:
    1. delete objectName;
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2010
    Posts
    100
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Memory raising forever in server thread application

    I tried that but I get a segmentation error because (I guess) I allocate the class in one function and I destroy it in another

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Memory raising forever in server thread application

    With threads, you want to close them first, gracefully, before deleting them.
    Always check if an object exists before deleting it.
    Manually delete those objects that are not child objects (as in the object that do not have a parent set).
    You can also look into deleteLater()

    Can you please use a QList or QVector for your list of threads?
    Using ServerThread[i] = new EFrwServer_thread(); is ugly. Note that you want to delete all the threads!

    The code you posted is not complete.
    What is connections[i] ? It's not defined.

  5. #5
    Join Date
    Jun 2010
    Posts
    100
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Memory raising forever in server thread application

    Quote Originally Posted by tbscope View Post
    With threads, you want to close them first, gracefully, before deleting them.
    Always check if an object exists before deleting it.
    Manually delete those objects that are not child objects (as in the object that do not have a parent set).
    You can also look into deleteLater()

    Can you please use a QList or QVector for your list of threads?
    Using ServerThread[i] = new EFrwServer_thread(); is ugly. Note that you want to delete all the threads!

    The code you posted is not complete.
    What is connections[i] ? It's not defined.
    Sorry for the delay but I was out of office and just arrived home now. I will try this tomorrow (hope it works)

    The connections[i] is just an array of qint16 to control the number of connections. You have as many connections positions as number of sql licenses and if the field is = 0 the "slot" (for the license) is free and if = 1 the license is busy. At the end of the thread a signal is emited and the array field goes back to 0.
    Besides setting the array field to 0 I need to delete the object because the memory is growing at a speed of 1MB per connection...which is also strange.

Similar Threads

  1. Memory leak in my threaded SSL server application
    By moosa in forum Qt Programming
    Replies: 8
    Last Post: 23rd August 2010, 07:49
  2. Replies: 1
    Last Post: 23rd April 2010, 13:23
  3. Replies: 3
    Last Post: 6th January 2010, 16:55
  4. Replies: 10
    Last Post: 22nd July 2009, 23:52
  5. why Qt's forever macro is for(;;) rather than while(true).
    By babu198649 in forum General Programming
    Replies: 1
    Last Post: 13th June 2009, 14:21

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.