Re: removeAll not working
Are you sure removeAll actually gets called? Could you put a qDebug statement inside the slot which does the removal? If removeAll() returns 1, then it did remove the item, maybe (because of some optimisations?) you get an invalid value for size of the list afterwards? Could you, after removal, iterate the list and see what elements it really contains?
Re: removeAll not working
Thanks for the incredibly quick reply!
> Are you sure removeAll actually gets called? Could you put a qDebug statement inside the slot which does the removal?
The qDebug statements I mentioned are actually in the slotOnDisconnect() slot, before and after the removeAll call (I should have made that clearer). So I am pretty sure that removeAll gets called.
> If removeAll() returns 1, then it did remove the item, maybe (because of some optimisations?) you get an invalid value for size of the list afterwards? Could you, after removal, iterate the list and see what elements it really contains?
I will do a little test iterating over the list, but I can tell you already that when I connect a client, close it, and then connect again, the client starts receiving all messages twice. Isn't that strange? Shouldn't I get a segfault for the following reason?
1. I connect a client, a MyTcpSocket object is created and a pointer is stored in the _clients list.
2. I close the client. The connectionClosed() -> deleteLater() connection deletes the MyTcpSocket, but removeAll doesn't remove the pointer from the _clients list.
3. I connect the client again, another MyTcpSocket object is created and its pointer stored in the _clients list. Now we have two items in the list, but one is pointing to a deleted object.
4. When sending a message to the clients a pointer to a deleted object gets used (step 2). Segfault?
It's as if the first MyTcpSocket never gets deleted and when a new client connects, the same socket is reused. Maybe that's causing the problem?
Thanks in advance for your interest
Re: removeAll not working
hello there..im also working with this fuction..
how can i list all connected client?
can u give me ur working example
Re: removeAll not working
Hi,
the problem is that you don't remove the client form the "real" list of clients.
Your clients method returns a copy of the clients list, your code would only work if you return a reference.
Changing
Code:
QList<MyTcpConnection*> MyTcpServer::clients()
to
Code:
QList<MyTcpConnection*>& MyTcpServer::clients()
should do the trick.