mas-bandwidth/yojimbo

Intermittent memory leak detected on client.Disconnect()

Closed this issue · 4 comments

kbirk commented

Hello,

I've been using libyojimbo for a small gamedev project. It's been great so far, however I've encountered an issue where occasionally when I call Disconnect I get the following output:

you leaked memory!

leaked block 0x7f693c2b2120 (2057 bytes) - yojimbo.cpp:3067

I'm not entirely sure what I've done that could be causing the leak, I've followed secure_client.cpp and USAGE.md exactly, the only difference is that I'm also using the network simulator to add some latency, packet loss, duplicates and jitter.

It happens about once in every ten or so disconnects. Other than the above error when I disconnect everything works perfectly. Memory usage of the program does not increase over time.

The loop / disconnect logic is about as straight forward as:

while (running) {
    client_.AdvanceTime(client_.GetTime() + Time::toSeconds(delta));
    client_.ReceivePackets();

    if (client_.IsDisconnected()) {
        break;
    }

    for (int32_t channel = 0; channel < config_.numChannels; channel++) {
        auto message = client_.ReceiveMessage(channel);
        while (message != nullptr) {
            processMessage(channel, message);
            client_.ReleaseMessage(message);
            message = client_.ReceiveMessage(channel);
        }
    }

    /// .. game logic

    client_.SendPackets();
}

client_.Disconnect();

Should I be calling something before Disconnect to ensure everything is cleaned up properly?

Is there anything I can do to help debug what I'm doing that is causing the leak?

Thank you for your time.

Can you please create a minimal example that reproduces this problem?

kbirk commented

So I was digging a bit deeper and realised that the leak only occurs if I use the network simulator over an unreliable unordered channel.

I've attached some code that when run as a test inside test.cpp can cause the leak.

Could it be that the network simulator may be holding onto certain packets to simulate latency or duplicates and those are not being properly deallocated before BaseClient::DestroyInternal() deallocates the network simulator?

sample.txt

Sounds likely!

Seems to be fixed