Memory leak while using freePlayer or removeAllMenuItems

Hi!

I'm encountering some C functions that lead to some memory leak.

My biggest issue right now is the freePlayer function that doesn't free all memory if a sound was loaded into the player.

Here's an example:

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

static PlaydateAPI* pd = NULL;
uint8_t test = 0;
FilePlayer* fp = NULL;

void setPDPtr(PlaydateAPI* p) {
    pd = p;
}

int update(void* ud)
{
	PDButtons current, pushed, released;
	pd->system->getButtonState(&current, &pushed, &released);
	if (pushed & kButtonA || pushed & kButtonB)
	{
		if (!test)
		{
			fp = pd->sound->fileplayer->newPlayer();
			pd->sound->fileplayer->loadIntoPlayer(fp, "Sound");
			pd->system->logToConsole("Sound loaded");
			test = 1;
		}
		else
		{
			pd->sound->fileplayer->freePlayer(fp);
			//pd->system->realloc(fp, 0); // <- Same result as previous line
			fp = NULL;
			pd->system->logToConsole("Freed");
			test = 0;
		}
	}
	return 0;
}

Pressing a button again and again will load a sound into a filePlayer then free the filePlayer, but memory will rise during this process. It seems an object isn't freed correctly.
Once again, this only occurs if a sound is actually loaded into the filePlayer, so you'll have to put a sound file to test this.

Other function, same issue with removeAllMenuItems. Here's a short code to test this:

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

static PlaydateAPI* pd = NULL;
uint8_t test = 0;
PDMenuItem *menuTest = NULL;

void setPDPtr(PlaydateAPI* p) {
    pd = p;
}

void testFn(void* userdata)
{
	pd->system->logToConsole("test");
}

int update(void* ud)
{
	PDButtons current, pushed, released;
	pd->system->getButtonState(&current, &pushed, &released);
	if (pushed & kButtonA || pushed & kButtonB)
	{
		if (!test)
		{
			menuTest = pd->system->addMenuItem("Test", testFn, NULL);
			pd->system->logToConsole("Menu added");
			test = 1;
		}
		else
		{
			pd->system->removeAllMenuItems();
			// pd->system->removeMenuItem(menuTest); // <- Doing this line instead works correctly
			pd->system->logToConsole("Menu removed");
			test = 0;
		}
	}
	return 0;
}

At least, the workaround is quite simple here: using removeMenuItem instead does free the memory correctly.

Can someone help me? Am I doing something wrong?
Thanks in advance!

Looks like @willco has a fix in for the menu item leak and it's scheduled for 1.13.4. I've just added a fix for the fileplayer leak (forgot to free the file path), and I'll see about getting that in there too. Thanks for pointing these out, and especially for the repro code! That saves us a huge amount of time.

Thanks for your answer! That's great news! I'll develop my game with more peace of mind knowing that those memory leaks will be fixed!

I did a dumb, for a while I stopped pushing fixes to the queue because it had gotten so backed up and just made notes in the issues pointing to my local branch, to push later when things were moving again. And then, of course, I totally forgot about them. :confused: Thanks for bringing this up again in the other thread, turns out the path leak fix didn't make it into the queue. It's in there now!