Jaskowicz1 / rconpp

A modern Source RCON library for C++
Apache License 2.0
10 stars 0 forks source link

fix: client crashes a program after destructor is called #14

Closed Kirkezz closed 2 months ago

Kirkezz commented 2 months ago

I am using your library as follows:

int getPlayersCount() {
    static int id = 3;
    int result = -1;

    rconpp::rcon_client client(rcon_credentials.ip, rcon_credentials.port, rcon_credentials.password);
    client.on_log = [](const std::string_view& log) { std::cout << log << std::endl; };
    client.start(true);
    if (client.connected) {
        auto response = client.send_data_sync("list", ++id, rconpp::data_type::SERVERDATA_EXECCOMMAND);
        /* ... */
        std::cout << response.data << std::endl;
    }

    return result;
}
int main() {
    ABMinecraft minecraft(Settings::rcon_credentials);
    minecraft.getPlayersCount();
    std::cout << "exited getPlayersCount(), rcon_client is destructed" << std::endl;
    while (true) {
    }
}

The program crashes a moment after the rcon_client is destructed: I think the problem is that queue_runner uses this->connected after the rcon_client object is destructed, and even if bool connected was set to false in the destructor, it may not be false after the memory is freed.

For a possible fix, see PR.

Jaskowicz1 commented 2 months ago

Thank you for your contribution!