GameWithPixels / DiceFirmware

Pixels dice firmware code.
MIT License
15 stars 1 forks source link

Heap memory crash #85

Open obasille opened 1 year ago

obasille commented 1 year ago

Allocating and freeing memory in a loop cause crashes (both in debug and release)

For example, call this function twice at the end of the init sequence.

uint32_t GetFreeMemorySize()
{
    uint32_t len = 0;
    while (len < 15000) // Don't go above 15k
    {
        len += 1024;
        NRF_LOG_INFO("Allocating %d", len);
        void *ptr = malloc(len);
        NRF_LOG_INFO("Got memory at %x", ptr);
        if (!ptr)
            break;
        free(ptr);
    }
    return len - 1024;
}
obasille commented 1 year ago

Calling the GetFreeMemorySize() twice yields those logs:

Allocating 1024
Got memory at 20003E20
Allocating 2048
Got memory at 20004228
Allocating 3072
Got memory at 20003E20
Allocating 4096
Got memory at 20004A30
Allocating 5120
Got memory at 20004630
Allocating 6144
Got memory at 20004230
Allocating 7168
Got memory at 20003E30
Allocating 8192
Got memory at 20005A38
Allocating 9216
Got memory at 20005638
Allocating 10240
Got memory at 20005238
Allocating 11264
Got memory at 20004E38
Allocating 12288
Got memory at 20004A38
Allocating 13312
Got memory at 20004638
Allocating 14336
Got memory at 20004238
Allocating 15360
Got memory at 20003
hardfault: HARD FAULT at 0x000281B2