S
S
Sazoks2019-09-22 07:47:38
C++ / C#
Sazoks, 2019-09-22 07:47:38

Does it throw an error when the thread terminates?

I have a client-server application. The client types some commands, they are sent to the server and then processed by this function:

void Server::ClientThread(SOCKET ClientConn)
{
  int ID = -1, r;	
  char Func[32];
        std::map<std::string, void(Server::*)(SOCKET, int)>::iterator It;
  while ((r = recv(ClientConn, Func, sizeof(Func) - 1, NULL)) != -1 && r != 0)
              if ((It = Functions.find(Func)) != Functions.end())
                    (this->*It->second)(ClientConn, ID);
  std::cout << "Client disconnected\n";
  if (ID != -1) DeleteUser(ID);
}

The function above runs on a new thread on every new connection.
I recently discovered that the thread is exiting with an error! Because of this, the program sometimes behaves strangely.
When exiting this function, and therefore, at the end of the stream, the error "symbols are not loaded _file name here_" pops up. Well, the first thing I did was download the symbols (seems logical), but now instead it says "Source code not available" and offers to look at the disassembled code. Oh yeah, that function above is NOT static. I pass it to the thread by method pointer along with this.
And yet, when exiting the thread in the debugger, I got into the xthread.
There is the following feature:
static unsigned int __stdcall _Call_func(void *_Data)
    {	// entry point for new thread
    static_cast<_Pad *>(_Data)->_Go();
    return (0);
    }

It is in the line return (0) that it gives me an error ..
In general, that's all.
I would be grateful for any help!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
maaGames, 2019-09-22
@maaGames

_Data == nullptr, so it crashes.
The debugger shows the NEXT line, after which the error occurred. Those. error before return. And there are only two options, either _Data == nullptr, or something went wrong in the _Go () function and it’s further poking around in the debugger :)

A
Armenian Radio, 2019-09-22
@gbg

The program is simply written incorrectly. recv can return any number of bytes, from 0 to 31. At the same time, your array is not initialized, and it is not clear whether it will contain 0.
When you try to search in the map, you get a string from the array of characters, the end of which is determined by the presence of zero. And zero may not be, in the end - a fall.
Moreover, having read only a piece of data, recv will read the continuation of this data on the next reading, that is, instead of the beginning of the next command, you will get the middle of the previous one, and as a result - complete garbage.
You need to rewrite the reading of commands and rewrite the protocol so that the length of the next command is transmitted at the beginning of this very command.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question