Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Apr 9, 2024

Conversation

Kirkezz
Copy link
Contributor

@Kirkezz Kirkezz commented Apr 9, 2024

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.

```
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
Copy link
Owner

Thank you for your contribution!

@Jaskowicz1 Jaskowicz1 merged commit d77993b into Jaskowicz1:main Apr 9, 2024
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants