Search code examples
esp32delete-operatorheap-corruptiondata-containers

ESP32 heap corruption error when releasing allocated memory


I am currently programming the ESP32 board in C++ and I am having trouble with my dataContainer class and releasing/allocating memory.

I do use the following DataContainer class (simplyfied):

    template <typename Elementtype>
class DataContainer
{
private:
    Elementtype **datalist;
    int maxsize;
    std::size_t currentsize; // How much data is saved in datalist

public:
    DataContainer(int maxcapacity);
    ~DataContainer();
    ...some methods...
    void reset_all_data();
};

And here is the reset_all_data() definition:

/* Deletes all Data of Datacontainer and allocates new memory*/
template <typename Elementtype>
void DataContainer<Elementtype>::reset_all_data()
{
    for (int i = 0; i < currentsize; i++)
    {
        if (datalist[i])
            Serial.println(heap_caps_check_integrity_all(true));

            delete datalist[i]; <-- Error is triggered here!!!

            Serial.println(heap_caps_check_integrity_all(true));
    }
    delete datalist;

    datalist = new Elementtype *[maxsize];
    for (int i = 0; i < maxsize; i++) // Declare a memory block of size maxsize (maxsize = 50)
    {
        datalist[i] = new Elementtype[5];
    }

    currentsize = 0;
}

As you can see, I have added some integrity checks, but the one before delete datalist (this seems to trigger the error). When I call reset_all_data() from my main.cpp at a certain point in my program the following error is triggered:

CORRUPT HEAP: Bad head at 0x3ffbb0f0. Expected 0xabba1234 got 0x3ffb9a34
assert failed: multi_heap_free multi_heap_poisoning.c:253 (head != NULL)
Backtrace:0x40083881:0x3ffb25400x4008e7e5:0x3ffb2560 0x40093d55:0x3ffb2580 0x4009399b:0x3ffb26b0 0x40083d41:0x3ffb26d0 
0x40093d85:0x3ffb26f0 0x4014e3f5:0x3ffb2710 0x400d2dc6:0x3ffb2730 0x400d31e3:0x3ffb2750 0x400d9b02:0x3ffb2820 

One more thing, the error is only triggered when a certain function is called right before it, even when the whole code inside this function is commented. This is the function's head: void write_data_container_to_file(fs::FS &fs, const char *path, DataContainer<uint16_t> data, const char *RTC_timestamp)thus, the mere call of the function plays an import role here.

Right now I am completely lost - any suggestion/idea is welcome on how to proceed.

EDIT: The dataContainer holds a 2D array of uint16_t.


Solution

  • I finally tracked down the, rather obvious, reason for the HEAP CORRUPTION. In the end I only called delete datalist but it would have been correct to call delete[] datalist after the for loop. The reason is, that within the for loop I delete the pointers pointing to arrays, which represent the "rows" of my allcoated 2D memory. In the end, I also have to delete the pointer, which points to the array holding the pointers I deleted within the for loop.

    So I was not paying attention and one should watch out that when it comes to releasing the previously allocated memory, care should be taken if delete or delete[]should be called.