Closed MilowB closed 2 years ago
Yeap, I do confirm this issue. I just got one of those segfaults.
I think that deleteClient
is not thread-safe at all:
bool TcpServer::deleteClient(Client & client) {
int clientIndex = -1;
for (uint i=0; i<m_clients.size(); i++) {
if (m_clients[i] == client) {
clientIndex = i;
break;
}
}
if (clientIndex > -1) {
m_clients.erase(m_clients.begin() + clientIndex);
return true;
}
return false;
}
I think that one way to easily fix it would be by enclosing writes on m_clients
on critical sections / mutex
thanks for the heads up guys. The code wasn't thread safe previously. In the new release I fixed some bugs and also made the code thread safe.
The server crash at random moments when receiving data from several clients. By running the debugger I found that it appears in the
TcpServer::deleteClient()
method. When a Client is erased from the clients vector the Client destructor is called. Then a segfault appears when the program tries todetach()
the thread.I believe it happens because the clients vector is modified by each thread when a client end the connection. I think a mutex could solve this issue to avoid changing the vector in several threads at the same time.