Search code examples
c++pluginswinsockrecv

BakkesMod winsock2 recv Crash


C++ BakkesMod Plugin using winsock2.h && Ws2tcpip.h && bakkesmod SDK (requires x64 Release builds only)

Problem: Crashes on recv(). Try-Catch doesn't stop it, threading doesn't stop it, all checks show it should work fine. Send works fine, and while I might be able to create a send-only ping, there are additional features I would like to use that require receiving small requests from the server.

Initialized Variables of Note:

// Socket.private
int sock;
std::thread* reader;
// Socket.public
std::function<void(void)> onConnect;
std::function<void(void)> onDisconnect;
std::function<void(std::string)> onMessageReceived;
std::function<void(ErrorType, std::string)> onError;

// Relevant Function(s)
int Socket::Connect(int tries) {
    if (tries <= 0)
        return -1;

    if (ConnectionState == 1)
        Disconnect();
    ConnectionState = 0;
    
    if (onError != NULL)
        onError(ErrorType::DEBUG, "Discord: Connecting...");
    struct sockaddr_in serv_addr;
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        if (onError != NULL)
            onError(ErrorType::CREATE_FAILED, "Discord: Could not Create Socket");

        if (onDisconnect != NULL)
            onDisconnect();

        return Connect(tries - 1);
    }
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(this->Port);

    // Convert IPv4 and IPv6 addresses from text to binary form 
    if (inet_pton(AF_INET, this->Address.c_str(), &serv_addr.sin_addr) <= 0)
    {
        if (onError != NULL)
            onError(ErrorType::BIND_FAILED, "Discord: Could not Bind Socket");

        if (onDisconnect != NULL)
            onDisconnect();

        return Connect(tries - 1);
    }
    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    {
        if (onError != NULL)
            onError(ErrorType::CONNECT_FAILED, "Discord: Could not Connect");

        if (onDisconnect != NULL)
            onDisconnect();

        return Connect(tries - 1);;
    }

    ConnectionState = 1;
    
    if (onError != NULL)
        onError(ErrorType::DEBUG, "Discord: Authenticating...");
    Send("AUTH " + this->AuthCode, 1);

    if (onConnect != NULL)
        onConnect();

    std::thread thread_obj([this] { this->Read(); });
    this->reader = &thread_obj;

    return 1;
}

Read Call:

#pragma warning( disable : 4700 )
void Socket::Read() {
    if (onError != NULL)
        onError(ErrorType::DEBUG, "Discord: Receive Thread Started");
    int len = 0;
    // Tried {0} to solve warning 4700, also left uninitialized with same result
    char* msg = { 0 };
    int bytesreceived = 0;
    while (ConnectionState == 1) {
        do {
            char buffer[MAX_BUFFER];
            int buflen = MAX_BUFFER;
            if (onError != NULL)
                onError(ErrorType::DEBUG, "Discord: Receiving...");
            try {
                // *CRASH LINE*
                len = recv(this->sock, buffer, buflen, 0);
            }
            catch (int e) {
                if (onError != NULL)
                    onError(ErrorType::DEBUG, "Discord: Receive Error (" + std::to_string(e) + ")");
            }

            if (onError != NULL)
                onError(ErrorType::DEBUG, "Discord: Received " + std::to_string(len) + " bytes");
            if (len > 0) {
                // Concatenate Buffer
            }
            if (onError != NULL)
                onError(ErrorType::DEBUG, "Discord: Buffered " + std::to_string(bytesreceived) + " bytes");
            // Parse for Command(s)
    }
    if (bytesreceived > 0) {
        if (onError != NULL)
            onError(ErrorType::NOT_CONNECTED, "Discord: Receive Closed with Data");
    }
    if (onError != NULL)
        onError(ErrorType::DEBUG, "Discord: Receive Thread Ended");
}

Debug Output:

[17:09:46] [bakkesmod] [class plg::FoxsLeaderboard] Discord: AutoConnecting
[17:09:46] [bakkesmod] [class plg::FoxsLeaderboard] Discord: Connecting...
[17:09:46] [bakkesmod] [class plg::FoxsLeaderboard] Discord: Authenticating...
[17:09:46] [bakkesmod] [class plg::FoxsLeaderboard] Discord Connected
[17:09:46] [bakkesmod] [class plg::FoxsLeaderboard] Discord: Receive Thread Started
[17:09:46] [bakkesmod] [class plg::FoxsLeaderboard] Discord: Receiving...

Solution

  • This has nothing to do with recv.

    std::thread thread_obj([this] { this->Read(); });
    this->reader = &thread_obj;
    
    return 1;
    

    This constructs a std::thread object, creating a new execution thread.

    Afterwards, this function immediately returns.

    Returning from the function destroys all objects in automatic scope. Including this std::thread object. It gets destroyed just like any other local variable.

    Destroying a std::thread that has a non-detached execution thread results in std::terminate getting called, terminating the entire program.

    Also, note that your reader pointer is a dangling pointer also, pointing to a destroyed object, after the function returns.