Dries
Dries

Reputation: 1005

abort() has been called on winsock recv() method

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

Answers (2)

Dror Harari
Dror Harari

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:

  1. Call receiveThread.detach() severing this object to the thread thus making it non-joinable so when it is destroyed nothing bad happens.
  2. If you want later to join the thread then allocate it on the heap with new - store its pointer somewhere so that later you can join it and then delete it.

Upvotes: 0

cos
cos

Reputation: 958

Call

receiveThread.detach();

after creating a thread. As it gets deleted on scope exit otherwise.

Upvotes: 3

Related Questions