`network->http->release` leaks?

Seeing this issue building against 2.7.4 on macOS. C's network->http->release seems to be leaking some memory. The example below leaks 72 bytes each time I press A. If I don't call network->http->release, it leaks a 9 and 352 byte object each time as well.

I know this is a pretty small leak for something that probably won't be happening too often, but maybe it's something I'm just missing?

#include <stdio.h>
#include <stdlib.h>

#include "pd_api.h"

PlaydateAPI *pd = NULL;

HTTPConnection *conn;

int update(void *ud);

#ifdef _WINDLL
__declspec(dllexport)
#endif
int
eventHandler(PlaydateAPI *playdate, PDSystemEvent event, uint32_t arg)
{
    (void)arg;

    if (event == kEventInit)
    {
        pd = playdate;

        pd->system->setUpdateCallback(update, NULL);

        pd->network->http->requestAccess(NULL, 0, true, NULL, NULL, NULL);
    }

    return 0;
}

int update(void *ud)
{
    PDButtons current, pushed, released;
    pd->system->getButtonState(&current, &pushed, &released);

    if (pushed & kButtonA)
    {
        if (conn != NULL)
        {
            pd->network->http->release(conn);
        }

        conn = pd->network->http->newConnection("test.com", 443, true);
    }

    return 1;
}

leaving a note here that I've filed it and put it at the top of my todo list. I'll let you know what I find!

3 Likes

Okay, so the 72 bytes are a struct containing the user callbacks, which we store separately because reasons. I put together a fix for the leak by deallocing that struct and clearing the pointer when you call http->release(), but the result of that would be you wouldn't get any callbacks from the connection after that. I can't decide whether that's appropriate or not. It seems weird for someone to expect the system to keep sending data back after they've said they're done with the connection.. but it does allow that currently.

I'm going to kick this one down the road and we'll see when it comes back up again.. It's only 72 bytes and, as you say, it shouldn't be happening too often. Thanks for finding it!

Makes sense to me, thanks for digging into it!