PDA

View Full Version : Memory raising forever in server thread application



ruben.rodrigues
1st July 2010, 13:31
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.


class class_tcpServer : QTcpServer {
Q_OBJECT
public:
class_tcpServer();
virtual ~class_tcpServer();

private:
QTcpServer *tcpServer;
class_tcpServer *ServerThread;

private slots:
void wait_for_data();
void free_slot(qint16);
};

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


void class_tcpServer::wait_for_data()
{
cout << "New connection made" << endl;

while(tcpServer->hasPendingConnections()){ //This controls pending connections

for (i = 0 ; i < max_connections ; i++) //Max connections is the number of sql licenses
{
if (connections[i] == 0)
{
cout << "Found empty slot for new connection" << endl;

ServerThread[i] = new EFrwServer_thread();

connect(ServerThread[i], SIGNAL(free_connection_slot(qint16)), this, SLOT(free_slot(qint16)));


ServerThread[i]->tcpSocket = tcpServer->nextPendingConnection();

ServerThread[i]->my_connection_number = i;

ServerThread[i]->start();

connections[i] = 1;
i = max_connections;
}
}
}
}

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

tbscope
1st July 2010, 15:08
private:
QTcpServer *tcpServer;
class_tcpServer *ServerThread;

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:

delete objectName;

ruben.rodrigues
1st July 2010, 15:26
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

tbscope
1st July 2010, 15:35
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.

ruben.rodrigues
1st July 2010, 21:54
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.