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(¤t, &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(¤t, &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!