I'm trying to use strdup to capture a result of JSON parsing. After using it, I'd like to free the memory. Doing to seems to misbehave in the Simulator but not on the device...
I minimized this to a simple strdup of a constant string, and it has the same effect: works fine on-device, crashes the Simulator instantly. Could this be something wrong with my setup or is the Simulator at fault? I would've expected it to work on both platforms.
Reproduction code:
#include "pd_api.h"
int update(void *userdata)
{
return 0;
}
int eventHandler(PlaydateAPI *pd, PDSystemEvent event, uint32_t arg)
{
if (event == kEventInit)
{
char *mystring = strdup("hello");
free(mystring);
pd->system->setUpdateCallback(update, pd);
}
return 0;
}
Edit: as a workaround, a custom strdup implementation seems to work fine. I'm worried, however, that a transitive call to strdup (e.g. in a third-party library) might still be problematic.
char *mystrdup(const char *s)
{
char *d = malloc(strlen(s) + 1);
if (d == NULL)
return NULL;
strcpy(d, s);
return d;
}