Reputation: 1005
I'm trying to receive messages from the server I'm building.
At a certain point, when the client has connected to the server, I start a receive thread which runs constantly. I start is with this code:
std::thread receiveThread(&NetworkingLib::Base::Receive, this);
It is called from a method within the same class the Receive() method is in.
Now after that it does go into the Receive() method but it crashes when the recv() method is being called (the Winsock one).
if (ConnectSocket != INVALID_SOCKET)
{
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
...
}
All those variables contain a working value though. The error says:
R6010 - abort() has been called.
I have no idea how to solve this or what might be the problem.
EDIT:
Here's some extra code to show how the socket was created and such. (I left out most of the error checking)
recvbuf and recvbuf are declared as follows:
int recvbuflen;
char recvbuf[512];
After that in the constructor, recvbuflen is declared as follows:
Base::Base(void):
recvbuflen(512)
{
...
}
Here's how I created the socket object:
// Create a SOCKET to connect to the server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
// Connect to server.
iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
After this has been done I start the receive thread. (The first piece of code in this post)
Upvotes: 4
Views: 1445
Reputation: 3287
The problem here is that the method which created the receiveThread object returned which resulted in the receiveThread object getting destroyed. Because receiveThread is joinable, then when it is destroyed, it calls terminate() which gives you the error.
You have two options:
receiveThread.detach()
severing this object to the thread thus making it non-joinable so when it is destroyed nothing bad happens. Upvotes: 0
Reputation: 958
Call
receiveThread.detach();
after creating a thread. As it gets deleted on scope exit otherwise.
Upvotes: 3