"free()" error, but "realloc(, 0)" works

,

Hi !

I'm currently using the C-API (don't know what tag to set ?) on Windows with CLion
And in my update function, I want to show formatted text.

So I'm using :

static int update(void* userdata) {
    PlaydateAPI* pd = userdata;

    ...

    int camX;
    int camY:

    // Set the variables "camX" and "camY" to the position of the camera
    camera_getPosition(&camX, &camY); 

    char* camPosStr;
    pd->system->formatString(&camPosStr, "Camera : %d, %d", camX, camY);

    pd->graphics->drawText(camPosStr, strlen(camPosStr), kASCIIEncoding, camX - 50, camY - 50);
    
    ...
}

but at the end of update, I need to free the memory (because formatString allocates a buffer).

But, when I'm trying to free it using :

free(camPosStr);

I get an error :
Error

But, when I'm using :

pd->system->realloc(camPosStr, 0);

It's working fine...

That's weird because free() is remapped to pd->system->realloc(x, 0) in setup.c ?
Is that a "normal" behavior, or just a bug ?

My guess is that VS is remapping free() to an internal free function to track memory allocations (can you disable leak detection?). But you've allocated the memory using the pd memory system so VS doesn't have a matching malloc() call so it ends up throwing an error.

+1

I was gonna say I think they grab that hook in the c runtime

Thanks, probably that.
I'll try to disable the protection, or I'm just going to use the playdate's allocation functions !

Yeah, as a rule I would just try to stick with using the pd memory functions