yojimbo icon indicating copy to clipboard operation
yojimbo copied to clipboard

Intermittent memory leak detected on client.Disconnect()

Open kbirk opened this issue 4 years ago • 3 comments

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.

kbirk avatar Feb 12 '21 02:02 kbirk

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

gafferongames avatar Feb 12 '21 03:02 gafferongames

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

kbirk avatar Feb 13 '21 02:02 kbirk

Sounds likely!

gafferongames avatar Feb 13 '21 03:02 gafferongames

Seems to be fixed

gafferongames avatar Dec 26 '23 04:12 gafferongames